src/audio/SDL_audio.c
author Ryan C. Gordon <icculus@icculus.org>
Sat, 06 Aug 2016 02:47:27 -0400
changeset 10244 aaed7b1f783a
parent 10242 48c3f46395ae
child 10246 db0b712b79dd
permissions -rw-r--r--
audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
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
/* !!! FIXME: These are wordy and unlocalized... */
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    37
#define DEFAULT_OUTPUT_DEVNAME "System audio output device"
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    38
#define DEFAULT_INPUT_DEVNAME "System audio capture device"
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    39
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
/*
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
    42
 * 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
    43
 *  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
    44
 *  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
    45
 */
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 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
    47
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
    48
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
    49
extern AudioBootStrap PULSEAUDIO_bootstrap;
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 3096
diff changeset
    50
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
    51
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
    52
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
    53
extern AudioBootStrap ESD_bootstrap;
8833
ae720d61d14d Chrome's Native Client backend implementation
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8761
diff changeset
    54
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
    55
extern AudioBootStrap NAS_bootstrap;
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents: 5588
diff changeset
    56
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
    57
extern AudioBootStrap DSOUND_bootstrap;
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents: 5586
diff changeset
    58
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
    59
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
    60
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
    61
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
    62
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
    63
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
    64
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
    65
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
    66
extern AudioBootStrap DART_bootstrap;
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2728
diff changeset
    67
extern AudioBootStrap NDSAUD_bootstrap;
2947
fec0db6c44b7 Date: Thu, 01 Jan 2009 21:32:12 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 2942
diff changeset
    68
extern AudioBootStrap FUSIONSOUND_bootstrap;
4718
79aa6c61f33b Added stub android sound system
Paul Hunkin <paul@bieh.net>
parents: 3699
diff changeset
    69
extern AudioBootStrap ANDROIDAUD_bootstrap;
7009
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents: 6977
diff changeset
    70
extern AudioBootStrap PSPAUD_bootstrap;
7367
d6eabafa8776 Added an SDL2 OpenBSD sndio(7) audio target.
Ryan C. Gordon <icculus@icculus.org>
parents: 7348
diff changeset
    71
extern AudioBootStrap SNDIO_bootstrap;
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9148
diff changeset
    72
extern AudioBootStrap EmscriptenAudio_bootstrap;
1190
173c063d4f55 OS/2 port!
Ryan C. Gordon <icculus@icculus.org>
parents: 955
diff changeset
    73
8833
ae720d61d14d Chrome's Native Client backend implementation
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8761
diff changeset
    74
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    75
/* Available audio drivers */
3162
dc1eb82ffdaa Von: Thomas Zimmermann
Sam Lantinga <slouken@libsdl.org>
parents: 3139
diff changeset
    76
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
    77
#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
    78
    &PULSEAUDIO_bootstrap,
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    79
#endif
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    80
#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
    81
    &ALSA_bootstrap,
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    82
#endif
7367
d6eabafa8776 Added an SDL2 OpenBSD sndio(7) audio target.
Ryan C. Gordon <icculus@icculus.org>
parents: 7348
diff changeset
    83
#if SDL_AUDIO_DRIVER_SNDIO
d6eabafa8776 Added an SDL2 OpenBSD sndio(7) audio target.
Ryan C. Gordon <icculus@icculus.org>
parents: 7348
diff changeset
    84
    &SNDIO_bootstrap,
d6eabafa8776 Added an SDL2 OpenBSD sndio(7) audio target.
Ryan C. Gordon <icculus@icculus.org>
parents: 7348
diff changeset
    85
#endif
4548
b135d19f9764 RedHat patch: SDL-1.2.14-audiodriver.patch
Sam Lantinga <slouken@libsdl.org>
parents: 4507
diff changeset
    86
#if SDL_AUDIO_DRIVER_BSD
b135d19f9764 RedHat patch: SDL-1.2.14-audiodriver.patch
Sam Lantinga <slouken@libsdl.org>
parents: 4507
diff changeset
    87
    &BSD_AUDIO_bootstrap,
b135d19f9764 RedHat patch: SDL-1.2.14-audiodriver.patch
Sam Lantinga <slouken@libsdl.org>
parents: 4507
diff changeset
    88
#endif
2939
084e5b4fc5be Audio driver priority: favor PulseAudio and ALSA over OSS.
Ryan C. Gordon <icculus@icculus.org>
parents: 2938
diff changeset
    89
#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
    90
    &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
    91
#endif
3099
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 3096
diff changeset
    92
#if SDL_AUDIO_DRIVER_QSA
82e60908fab1 Date: Mon, 23 Mar 2009 09:17:24 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 3096
diff changeset
    93
    &QSAAUDIO_bootstrap,
663
8bedd6d61642 Date: Sat, 2 Aug 2003 16:22:51 +0300
Sam Lantinga <slouken@libsdl.org>
parents: 654
diff changeset
    94
#endif
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    95
#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
    96
    &SUNAUDIO_bootstrap,
148
8758b8d42cd9 Audio subsystem no longer assumes sun audio API on UNIX systems
Sam Lantinga <slouken@libsdl.org>
parents: 121
diff changeset
    97
#endif
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    98
#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
    99
    &ARTS_bootstrap,
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   100
#endif
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   101
#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
   102
    &ESD_bootstrap,
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   103
#endif
8833
ae720d61d14d Chrome's Native Client backend implementation
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8761
diff changeset
   104
#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
   105
    &NACLAUD_bootstrap,
8833
ae720d61d14d Chrome's Native Client backend implementation
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8761
diff changeset
   106
#endif
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   107
#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
   108
    &NAS_bootstrap,
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   109
#endif
5592
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents: 5588
diff changeset
   110
#if SDL_AUDIO_DRIVER_XAUDIO2
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents: 5588
diff changeset
   111
    &XAUDIO2_bootstrap,
2e88d0742f4d Implemented XAudio2 target for Windows (and Xbox360, theoretically!).
Ryan C. Gordon <icculus@icculus.org>
parents: 5588
diff changeset
   112
#endif
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   113
#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
   114
    &DSOUND_bootstrap,
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   115
#endif
5588
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents: 5586
diff changeset
   116
#if SDL_AUDIO_DRIVER_WINMM
57bfc2a2a452 Reworked Windows waveOut code.
Ryan C. Gordon <icculus@icculus.org>
parents: 5586
diff changeset
   117
    &WINMM_bootstrap,
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   118
#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
   119
#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
   120
    &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
   121
#endif
7981
6d538bc1b395 Renamed things named after BeOS to be named after Haiku instead.
Ryan C. Gordon <icculus@icculus.org>
parents: 7904
diff changeset
   122
#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
   123
    &HAIKUAUDIO_bootstrap,
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   124
#endif
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   125
#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
   126
    &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
   127
#endif
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   128
#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
   129
    &DISKAUD_bootstrap,
68
ac6645260d31 Added an audio driver that writes to disk (thanks Ryan!)
Sam Lantinga <slouken@lokigames.com>
parents: 38
diff changeset
   130
#endif
1532
30f189cdd82b Implemented dummy audio driver.
Ryan C. Gordon <icculus@icculus.org>
parents: 1487
diff changeset
   131
#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
   132
    &DUMMYAUD_bootstrap,
1532
30f189cdd82b Implemented dummy audio driver.
Ryan C. Gordon <icculus@icculus.org>
parents: 1487
diff changeset
   133
#endif
2947
fec0db6c44b7 Date: Thu, 01 Jan 2009 21:32:12 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 2942
diff changeset
   134
#if SDL_AUDIO_DRIVER_FUSIONSOUND
fec0db6c44b7 Date: Thu, 01 Jan 2009 21:32:12 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 2942
diff changeset
   135
    &FUSIONSOUND_bootstrap,
fec0db6c44b7 Date: Thu, 01 Jan 2009 21:32:12 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 2942
diff changeset
   136
#endif
4718
79aa6c61f33b Added stub android sound system
Paul Hunkin <paul@bieh.net>
parents: 3699
diff changeset
   137
#if SDL_AUDIO_DRIVER_ANDROID
79aa6c61f33b Added stub android sound system
Paul Hunkin <paul@bieh.net>
parents: 3699
diff changeset
   138
    &ANDROIDAUD_bootstrap,
79aa6c61f33b Added stub android sound system
Paul Hunkin <paul@bieh.net>
parents: 3699
diff changeset
   139
#endif
7009
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents: 6977
diff changeset
   140
#if SDL_AUDIO_DRIVER_PSP
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents: 6977
diff changeset
   141
    &PSPAUD_bootstrap,
161b7b6a5303 Add PSP support
Captain Lex <kimonline@126.com>
parents: 6977
diff changeset
   142
#endif
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9148
diff changeset
   143
#if SDL_AUDIO_DRIVER_EMSCRIPTEN
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9148
diff changeset
   144
    &EmscriptenAudio_bootstrap,
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9148
diff changeset
   145
#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
   146
    NULL
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   147
};
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   148
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   149
static SDL_AudioDevice *
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   150
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
   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
    id--;
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   153
    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
   154
        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
   155
        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
   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
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   158
    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
   159
}
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   160
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   161
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   162
/* 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
   163
static void
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   164
SDL_AudioDetectDevices_Default(void)
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   165
{
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   166
    /* 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
   167
    SDL_assert(current_audio.impl.OnlyHasDefaultOutputDevice);
10240
0ba7106e9a6d audio: Changed OnlyHasDefaultInputDevice to OnlyHasDefaultCaptureDevice.
Ryan C. Gordon <icculus@icculus.org>
parents: 10238
diff changeset
   168
    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
   169
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   170
    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
   171
    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
   172
        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
   173
    }
2060
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_AudioThreadInit_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_AudioWaitDevice_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
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   186
static void
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   187
SDL_AudioPlayDevice_Default(_THIS)
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   188
{                               /* no-op. */
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   189
}
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2728
diff changeset
   190
9031
e963a13a720c Added a GetPendingBytes method to the audio backend.
Ryan C. Gordon <icculus@icculus.org>
parents: 9012
diff changeset
   191
static int
e963a13a720c Added a GetPendingBytes method to the audio backend.
Ryan C. Gordon <icculus@icculus.org>
parents: 9012
diff changeset
   192
SDL_AudioGetPendingBytes_Default(_THIS)
e963a13a720c Added a GetPendingBytes method to the audio backend.
Ryan C. Gordon <icculus@icculus.org>
parents: 9012
diff changeset
   193
{
e963a13a720c Added a GetPendingBytes method to the audio backend.
Ryan C. Gordon <icculus@icculus.org>
parents: 9012
diff changeset
   194
    return 0;
e963a13a720c Added a GetPendingBytes method to the audio backend.
Ryan C. Gordon <icculus@icculus.org>
parents: 9012
diff changeset
   195
}
e963a13a720c Added a GetPendingBytes method to the audio backend.
Ryan C. Gordon <icculus@icculus.org>
parents: 9012
diff changeset
   196
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   197
static Uint8 *
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   198
SDL_AudioGetDeviceBuf_Default(_THIS)
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   199
{
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   200
    return NULL;
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
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   203
static void
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   204
SDL_AudioWaitDone_Default(_THIS)
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   205
{                               /* no-op. */
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   206
}
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2728
diff changeset
   207
10221
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   208
static int
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   209
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
   210
{
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   211
    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
   212
}
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   213
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   214
static void
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   215
SDL_AudioFlushCapture_Default(_THIS)
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   216
{                               /* no-op. */
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   217
}
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
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_AudioCloseDevice_Default(_THIS)
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
}
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2728
diff changeset
   223
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   224
static void
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   225
SDL_AudioDeinitialize_Default(void)
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   226
{                               /* no-op. */
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   227
}
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   228
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   229
static void
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   230
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
   231
{                               /* no-op. */
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   232
}
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   233
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   234
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   235
static int
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   236
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
   237
{
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   238
    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
   239
}
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   240
9010
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   241
static SDL_INLINE SDL_bool
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   242
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
   243
{
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   244
    /* 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
   245
       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
   246
       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
   247
       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
   248
    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
   249
        return SDL_TRUE;
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   250
    }
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   251
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   252
    return SDL_FALSE;
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   253
}
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   254
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
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
   256
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
   257
{
9010
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   258
    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
   259
        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
   260
    }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   261
}
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   262
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   263
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
   264
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
   265
{
9010
6d059ed9b6ca Clarifying hard-to-understand piece of code.
Ryan C. Gordon <icculus@icculus.org>
parents: 8919
diff changeset
   266
    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
   267
        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
   268
    }
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
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   271
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   272
static void
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   273
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
   274
{
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   275
    /*
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   276
     * 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
   277
     *  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
   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
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   280
#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
   281
        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
   282
            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
   283
        }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   284
    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
   285
    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
   286
    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
   287
    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
   288
    FILL_STUB(PlayDevice);
9031
e963a13a720c Added a GetPendingBytes method to the audio backend.
Ryan C. Gordon <icculus@icculus.org>
parents: 9012
diff changeset
   289
    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
   290
    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
   291
    FILL_STUB(WaitDone);
10221
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   292
    FILL_STUB(CaptureFromDevice);
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   293
    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
   294
    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
   295
    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
   296
    FILL_STUB(UnlockDevice);
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   297
    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
   298
    FILL_STUB(Deinitialize);
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   299
#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
   300
}
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   301
2716
f8f68f47285a Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2274
diff changeset
   302
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   303
/* device hotplug support... */
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   304
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   305
static int
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   306
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
   307
{
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   308
    int retval = -1;
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   309
    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
   310
    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
   311
    if (item == NULL) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   312
        return -1;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   313
    }
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   314
9410
e25530faeda0 Audio hotplug fixes for winmm and XAudio2 backends.
Ryan C. Gordon <icculus@icculus.org>
parents: 9405
diff changeset
   315
    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
   316
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   317
    item->handle = handle;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   318
    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
   319
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   320
    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
   321
    item->next = *devices;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   322
    *devices = item;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   323
    retval = (*devCount)++;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   324
    SDL_UnlockMutex(current_audio.detectionLock);
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   325
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   326
    return retval;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   327
}
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   328
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   329
static SDL_INLINE int
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   330
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
   331
{
10213
4b0893d63092 audio: Initial bits to enable audio capture support.
Ryan C. Gordon <icculus@icculus.org>
parents: 10146
diff changeset
   332
    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
   333
    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
   334
}
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   335
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   336
static SDL_INLINE int
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   337
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
   338
{
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   339
    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
   340
}
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   341
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   342
static void
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   343
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
   344
{
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   345
    SDL_AudioDeviceItem *item, *next;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   346
    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
   347
        next = item->next;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   348
        if (item->handle != NULL) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   349
            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
   350
        }
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   351
        SDL_free(item);
9393
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
    *devices = NULL;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   354
    *devCount = 0;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   355
}
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   356
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   357
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   358
/* 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
   359
void
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   360
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
   361
{
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   362
    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
   363
    if (device_index != -1) {
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   364
        /* Post the event, if desired */
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   365
        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
   366
            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
   367
            SDL_zero(event);
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   368
            event.adevice.type = SDL_AUDIODEVICEADDED;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   369
            event.adevice.which = device_index;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   370
            event.adevice.iscapture = iscapture;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   371
            SDL_PushEvent(&event);
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   372
        }
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   373
    }
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   374
}
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   375
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   376
/* 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
   377
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
   378
{
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   379
    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
   380
10220
6fa358b97f4b audio: Made some SDL_AudioDevice fields atomic.
Ryan C. Gordon <icculus@icculus.org>
parents: 10217
diff changeset
   381
    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
   382
        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
   383
    }
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   384
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   385
    /* 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
   386
       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
   387
    current_audio.impl.LockDevice(device);
10220
6fa358b97f4b audio: Made some SDL_AudioDevice fields atomic.
Ryan C. Gordon <icculus@icculus.org>
parents: 10217
diff changeset
   388
    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
   389
    current_audio.impl.UnlockDevice(device);
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   390
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   391
    /* 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
   392
    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
   393
        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
   394
        SDL_zero(event);
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   395
        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
   396
        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
   397
        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
   398
        SDL_PushEvent(&event);
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
}
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   401
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   402
static void
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   403
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
   404
{
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   405
    SDL_AudioDeviceItem *item;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   406
    SDL_assert(handle != NULL);
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   407
    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
   408
        if (item->handle == handle) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   409
            item->handle = NULL;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   410
            *removedFlag = SDL_TRUE;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   411
            return;
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   412
        }
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   413
    }
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   414
}
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   415
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   416
/* 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
   417
void
9399
a684dbd185c7 SDL_RemoveAudioDevice() should specify capture vs output.
Ryan C. Gordon <icculus@icculus.org>
parents: 9398
diff changeset
   418
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
   419
{
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   420
    SDL_LockMutex(current_audio.detectionLock);
9399
a684dbd185c7 SDL_RemoveAudioDevice() should specify capture vs output.
Ryan C. Gordon <icculus@icculus.org>
parents: 9398
diff changeset
   421
    if (iscapture) {
a684dbd185c7 SDL_RemoveAudioDevice() should specify capture vs output.
Ryan C. Gordon <icculus@icculus.org>
parents: 9398
diff changeset
   422
        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
   423
    } else {
a684dbd185c7 SDL_RemoveAudioDevice() should specify capture vs output.
Ryan C. Gordon <icculus@icculus.org>
parents: 9398
diff changeset
   424
        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
   425
    }
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   426
    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
   427
    current_audio.impl.FreeDeviceHandle(handle);
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   428
}
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   429
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   430
9012
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   431
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   432
/* buffer queueing support... */
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   433
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   434
/* 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
   435
static void
10244
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   436
free_audio_queue(SDL_AudioBufferQueue *packet)
9012
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   437
{
10244
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   438
    while (packet) {
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   439
        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
   440
        SDL_free(packet);
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   441
        packet = next;
9012
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   442
    }
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
10244
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   445
/* 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
   446
static int
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   447
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
   448
{
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   449
    SDL_AudioBufferQueue *orighead;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   450
    SDL_AudioBufferQueue *origtail;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   451
    Uint32 origlen;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   452
    Uint32 datalen;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   453
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   454
    orighead = device->buffer_queue_head;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   455
    origtail = device->buffer_queue_tail;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   456
    origlen = origtail ? origtail->datalen : 0;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   457
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   458
    while (len > 0) {
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   459
        SDL_AudioBufferQueue *packet = device->buffer_queue_tail;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   460
        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
   461
        if (!packet || (packet->datalen >= SDL_AUDIOBUFFERQUEUE_PACKETLEN)) {
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   462
            /* 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
   463
            packet = device->buffer_queue_pool;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   464
            if (packet != NULL) {
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   465
                /* we have one available in the pool. */
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   466
                device->buffer_queue_pool = packet->next;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   467
            } else {
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   468
                /* Have to allocate a new one! */
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   469
                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
   470
                if (packet == NULL) {
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   471
                    /* 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
   472
                    if (!origtail) {
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   473
                        packet = device->buffer_queue_head;  /* whole queue. */
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   474
                    } else {
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   475
                        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
   476
                        origtail->next = NULL;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   477
                        origtail->datalen = origlen;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   478
                    }
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   479
                    device->buffer_queue_head = orighead;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   480
                    device->buffer_queue_tail = origtail;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   481
                    device->buffer_queue_pool = NULL;
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
                    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
   484
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   485
                    return SDL_OutOfMemory();
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
            }
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   488
            packet->datalen = 0;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   489
            packet->startpos = 0;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   490
            packet->next = NULL;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   491
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   492
            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
   493
            if (device->buffer_queue_tail == NULL) {
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   494
                device->buffer_queue_head = packet;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   495
            } else {
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   496
                device->buffer_queue_tail->next = packet;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   497
            }
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   498
            device->buffer_queue_tail = packet;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   499
        }
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   500
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   501
        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
   502
        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
   503
        data += datalen;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   504
        len -= datalen;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   505
        packet->datalen += datalen;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   506
        device->queued_bytes += datalen;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   507
    }
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   508
10244
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   509
    return 0;
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
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   512
/* 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
   513
static Uint32
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   514
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
   515
{
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   516
    SDL_AudioBufferQueue *packet;
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   517
    Uint8 *ptr = stream;
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
    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
   520
        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
   521
        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
   522
        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
   523
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   524
        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
   525
        packet->startpos += cpy;
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   526
        ptr += cpy;
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   527
        device->queued_bytes -= cpy;
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   528
        len -= cpy;
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   529
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   530
        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
   531
            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
   532
            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
   533
            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
   534
            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
   535
        }
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   536
    }
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
    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
   539
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   540
    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
   541
        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
   542
    }
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   543
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   544
    return (Uint32) (ptr - stream);
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   545
}
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   546
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   547
static void SDLCALL
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   548
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
   549
{
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   550
    /* 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
   551
    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
   552
    Uint32 written;
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   553
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   554
    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
   555
    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
   556
    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
   557
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   558
    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
   559
    stream += written;
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   560
    len -= (int) written;
9012
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   561
10244
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   562
    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
   563
        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
   564
        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
   565
    }
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   566
}
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   567
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   568
static void SDLCALL
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   569
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
   570
{
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   571
    /* 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
   572
    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
   573
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   574
    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
   575
    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
   576
    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
   577
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   578
    /* 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
   579
       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
   580
       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
   581
    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
   582
}
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   583
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   584
int
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   585
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
   586
{
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   587
    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
   588
    int rc = 0;
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   589
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   590
    if (!device) {
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   591
        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
   592
    } else if (device->iscapture) {
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   593
        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
   594
    } 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
   595
        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
   596
    }
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
    if (len > 0) {
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   599
        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
   600
        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
   601
        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
   602
    }
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   603
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   604
    return rc;
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   605
}
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   606
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   607
Uint32
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   608
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
   609
{
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   610
    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
   611
    Uint32 rc;
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   612
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   613
    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
   614
         (!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
   615
         (!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
   616
         (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
   617
        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
   618
    }
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   619
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   620
    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
   621
    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
   622
    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
   623
    return rc;
9012
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   624
}
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   625
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   626
Uint32
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   627
SDL_GetQueuedAudioSize(SDL_AudioDeviceID devid)
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   628
{
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   629
    Uint32 retval = 0;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   630
    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
   631
10244
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   632
    if (!device) {
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   633
        return 0;
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   634
    }
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   635
9032
36c472422d64 SDL_GetQueuedAudioSize() shouldn't grab lock when not set up for queueing.
Ryan C. Gordon <icculus@icculus.org>
parents: 9031
diff changeset
   636
    /* 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
   637
    if (device->spec.callback == SDL_BufferQueueDrainCallback) {
9012
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   638
        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
   639
        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
   640
        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
   641
    } 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
   642
        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
   643
        retval = device->queued_bytes;
aaed7b1f783a audio: Implemented buffer queueing for capture devices (SDL_DequeueAudio()).
Ryan C. Gordon <icculus@icculus.org>
parents: 10242
diff changeset
   644
        current_audio.impl.UnlockDevice(device);
9012
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   645
    }
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   646
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   647
    return retval;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   648
}
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   649
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   650
void
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   651
SDL_ClearQueuedAudio(SDL_AudioDeviceID devid)
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   652
{
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   653
    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
   654
    SDL_AudioBufferQueue *packet;
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   655
9012
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   656
    if (!device) {
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   657
        return;  /* nothing to do. */
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   658
    }
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   659
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   660
    /* 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
   661
    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
   662
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   663
    /* 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
   664
    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
   665
    if (packet) {
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   666
        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
   667
    } else {
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   668
        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
   669
    }
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   670
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   671
    /* 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
   672
    device->buffer_queue_tail = NULL;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   673
    device->buffer_queue_head = NULL;
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   674
    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
   675
    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
   676
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   677
    /* 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
   678
    if (packet) {
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   679
        if (!packet->next) {
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   680
            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
   681
        } else {
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   682
            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
   683
            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
   684
            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
   685
        }
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   686
    }
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   687
9012
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   688
    current_audio.impl.UnlockDevice(device);
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   689
10242
48c3f46395ae audio: SDL_ClearQueuedAudio() should free everything but two packets.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   690
    /* 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
   691
    free_audio_queue(packet);
9012
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   692
}
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   693
aa058c87737b Added audio device buffer queueing API.
Ryan C. Gordon <icculus@icculus.org>
parents: 9010
diff changeset
   694
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   695
/* 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
   696
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
   697
SDL_RunAudio(void *devicep)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   698
{
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   699
    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
   700
    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
   701
    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
   702
    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
   703
    Uint8 *stream;
9398
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   704
    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
   705
    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
   706
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   707
    SDL_assert(!device->iscapture);
2716
f8f68f47285a Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2274
diff changeset
   708
5509
5b1b4d820d10 The API sets the priority for the current thread, not an arbitrary thread.
Sam Lantinga <slouken@libsdl.org>
parents: 5506
diff changeset
   709
    /* 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
   710
    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
   711
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
   712
    /* 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
   713
    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
   714
    current_audio.impl.ThreadInit(device);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   715
9398
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   716
    /* 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
   717
    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
   718
        /* 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
   719
        if (device->convert.needed) {
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   720
            stream = device->convert.buf;
10220
6fa358b97f4b audio: Made some SDL_AudioDevice fields atomic.
Ryan C. Gordon <icculus@icculus.org>
parents: 10217
diff changeset
   721
        } 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
   722
            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
   723
        } else {
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   724
            /* 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
   725
               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
   726
               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
   727
               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
   728
               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
   729
            stream = NULL;
2716
f8f68f47285a Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2274
diff changeset
   730
        }
3336
00fab0ebfe54 Fixed a bug where when the audio starts paused all the DirectSound buffers
Sam Lantinga <slouken@libsdl.org>
parents: 3283
diff changeset
   731
9398
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   732
        if (stream == NULL) {
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   733
            stream = device->fake_stream;
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   734
        }
2716
f8f68f47285a Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2274
diff changeset
   735
9398
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   736
        /* !!! 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
   737
        SDL_LockMutex(device->mixer_lock);
10220
6fa358b97f4b audio: Made some SDL_AudioDevice fields atomic.
Ryan C. Gordon <icculus@icculus.org>
parents: 10217
diff changeset
   738
        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
   739
            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
   740
        } else {
10221
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   741
            (*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
   742
        }
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   743
        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
   744
9398
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   745
        /* Convert the audio if necessary */
10220
6fa358b97f4b audio: Made some SDL_AudioDevice fields atomic.
Ryan C. Gordon <icculus@icculus.org>
parents: 10217
diff changeset
   746
        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
   747
            SDL_ConvertAudio(&device->convert);
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   748
            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
   749
            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
   750
                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
   751
            } else {
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   752
                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
   753
                           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
   754
            }
9398
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   755
        }
1562
1d487a3fab41 Fixed bug #18
Sam Lantinga <slouken@libsdl.org>
parents: 1561
diff changeset
   756
9398
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   757
        /* 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
   758
        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
   759
            SDL_Delay(delay);
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   760
        } else {
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   761
            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
   762
            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
   763
        }
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
   764
    }
1562
1d487a3fab41 Fixed bug #18
Sam Lantinga <slouken@libsdl.org>
parents: 1561
diff changeset
   765
9398
c41dd34e4996 Removed the broken audio streaming code, other small cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 9397
diff changeset
   766
    /* 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
   767
    /* !!! 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
   768
    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
   769
9382
1c0f6952e65e Cleaning out a silly coding style I used to use.
Ryan C. Gordon <icculus@icculus.org>
parents: 9381
diff changeset
   770
    return 0;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   771
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   772
10221
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   773
/* 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
   774
static int SDLCALL
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   775
SDL_CaptureAudio(void *devicep)
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   776
{
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   777
    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
   778
    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
   779
    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
   780
    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
   781
    Uint8 *stream;
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   782
    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
   783
    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
   784
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   785
    SDL_assert(device->iscapture);
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   786
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   787
    /* 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
   788
    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
   789
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   790
    /* Perform any thread setup */
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   791
    device->threadid = SDL_ThreadID();
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   792
    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
   793
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   794
    /* 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
   795
    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
   796
        int still_need;
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   797
        Uint8 *ptr;
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
        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
   800
            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
   801
            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
   802
            continue;
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   803
        }
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   804
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   805
        /* 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
   806
        still_need = stream_len;
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   807
        if (device->convert.needed) {
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   808
            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
   809
        } else {
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   810
            /* 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
   811
            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
   812
        }
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   813
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   814
        /* 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
   815
           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
   816
           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
   817
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   818
        while (still_need > 0) {
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   819
            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
   820
            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
   821
            if (rc > 0) {
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   822
                still_need -= rc;
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   823
                ptr += rc;
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   824
            } 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
   825
                SDL_OpenedAudioDeviceDisconnected(device);
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   826
                break;
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   827
            }
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 (still_need > 0) {
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   831
            /* 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
   832
            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
   833
        }
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   834
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   835
        if (device->convert.needed) {
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   836
            SDL_ConvertAudio(&device->convert);
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   837
        }
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   838
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   839
        /* !!! 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
   840
        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
   841
        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
   842
            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
   843
        } else {
10221
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   844
            (*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
   845
        }
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   846
        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
   847
    }
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   848
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   849
    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
   850
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   851
    return 0;
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   852
}
04cda108b406 audio: implemented higher level infrastructure for running capture devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   853
322
fd93a09655e3 The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   854
1982
3b4ce57c6215 First shot at new audio data types (int32 and float32).
Ryan C. Gordon <icculus@icculus.org>
parents: 1978
diff changeset
   855
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
   856
SDL_ParseAudioFormat(const char *string)
1794
5605a9820134 Fixed bug #137
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   857
{
2076
939420dee849 Use SDL_strcmp(), not strcmp().
Ryan C. Gordon <icculus@icculus.org>
parents: 2070
diff changeset
   858
#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
   859
    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
   860
    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
   861
    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
   862
    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
   863
    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
   864
    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
   865
    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
   866
    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
   867
    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
   868
    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
   869
    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
   870
    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
   871
    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
   872
    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
   873
    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
   874
    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
   875
    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
   876
    CHECK_FMT_STRING(F32);
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   877
#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
   878
    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
   879
}
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
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
   882
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
   883
{
9382
1c0f6952e65e Cleaning out a silly coding style I used to use.
Ryan C. Gordon <icculus@icculus.org>
parents: 9381
diff changeset
   884
    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
   885
}
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
   886
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
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
   888
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
   889
{
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
   890
    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
   891
        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
   892
    }
9382
1c0f6952e65e Cleaning out a silly coding style I used to use.
Ryan C. Gordon <icculus@icculus.org>
parents: 9381
diff changeset
   893
    return NULL;
1794
5605a9820134 Fixed bug #137
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   894
}
5605a9820134 Fixed bug #137
Sam Lantinga <slouken@libsdl.org>
parents: 1769
diff changeset
   895
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
   896
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
   897
SDL_AudioInit(const char *driver_name)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   898
{
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   899
    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
   900
    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
   901
    int tried_to_init = 0;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   902
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   903
    if (SDL_WasInit(SDL_INIT_AUDIO)) {
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2053
diff changeset
   904
        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
   905
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   906
9392
92e23eff9b89 Changed some SDL_memset() calls to SDL_zero().
Ryan C. Gordon <icculus@icculus.org>
parents: 9391
diff changeset
   907
    SDL_zero(current_audio);
92e23eff9b89 Changed some SDL_memset() calls to SDL_zero().
Ryan C. Gordon <icculus@icculus.org>
parents: 9391
diff changeset
   908
    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
   909
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
   910
    /* 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
   911
    if (driver_name == NULL) {
8f1ab2f7c722 Look at environment variables in SDL_VideoInit() and SDL_AudioInit()
Sam Lantinga <slouken@libsdl.org>
parents: 1903
diff changeset
   912
        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
   913
    }
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   914
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   915
    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
   916
        /* 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
   917
        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
   918
        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
   919
            (!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
   920
            continue;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   921
        }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   922
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   923
        tried_to_init = 1;
9392
92e23eff9b89 Changed some SDL_memset() calls to SDL_zero().
Ryan C. Gordon <icculus@icculus.org>
parents: 9391
diff changeset
   924
        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
   925
        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
   926
        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
   927
        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
   928
    }
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   929
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   930
    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
   931
        /* 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
   932
        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
   933
            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
   934
                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
   935
            } 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
   936
                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
   937
            }
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
   938
        }
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   939
9392
92e23eff9b89 Changed some SDL_memset() calls to SDL_zero().
Ryan C. Gordon <icculus@icculus.org>
parents: 9391
diff changeset
   940
        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
   941
        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
   942
    }
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   943
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   944
    current_audio.detectionLock = SDL_CreateMutex();
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   945
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   946
    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
   947
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   948
    /* 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
   949
    current_audio.impl.DetectDevices();
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
   950
9382
1c0f6952e65e Cleaning out a silly coding style I used to use.
Ryan C. Gordon <icculus@icculus.org>
parents: 9381
diff changeset
   951
    return 0;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   952
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   953
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
   954
/*
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
   955
 * 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
   956
 */
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
   957
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
   958
SDL_GetCurrentAudioDriver()
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   959
{
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   960
    return current_audio.name;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   961
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   962
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   963
/* 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
   964
static void
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   965
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
   966
{
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   967
    SDL_AudioDeviceItem *item = *devices;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   968
    SDL_AudioDeviceItem *prev = NULL;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   969
    int total = 0;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   970
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   971
    while (item) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   972
        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
   973
        if (item->handle != NULL) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   974
            total++;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   975
            prev = item;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   976
        } else {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   977
            if (prev) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   978
                prev->next = next;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   979
            } else {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   980
                *devices = next;
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
            SDL_free(item);
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   983
        }
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   984
        item = next;
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
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   987
    *devCount = total;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   988
    *removedFlag = SDL_FALSE;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   989
}
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   990
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   991
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
   992
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
   993
SDL_GetNumAudioDevices(int iscapture)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
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
    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
   996
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
   997
    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
   998
        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
   999
    }
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
  1000
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
  1001
    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
  1002
    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
  1003
        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
  1004
    }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1005
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
  1006
    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
  1007
        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
  1008
        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
  1009
    }
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5592
diff changeset
  1010
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
  1011
    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
  1012
    SDL_UnlockMutex(current_audio.detectionLock);
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
  1013
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
  1014
    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
  1015
}
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1016
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
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
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
  1019
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
  1020
{
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
  1021
    const char *retval = NULL;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9392
diff changeset
  1022
2049
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 (!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
  1024
        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
  1025
        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
  1026
    }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1027
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1028
    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
  1029
        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
  1030
        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
  1031
    }
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2045
diff changeset
  1032
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
  1033
    if (index >= 0) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
  1034
        SDL_AudioDeviceItem *item;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
  1035
        int i;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
  1036
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
  1037
        SDL_LockMutex(current_audio.detectionLock);