SDL  2.0
SDL_sysaudio.h
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "../SDL_internal.h"
22 
23 #ifndef SDL_sysaudio_h_
24 #define SDL_sysaudio_h_
25 
26 #include "SDL_mutex.h"
27 #include "SDL_thread.h"
28 #include "../SDL_dataqueue.h"
29 #include "./SDL_audio_c.h"
30 
31 /* !!! FIXME: These are wordy and unlocalized... */
32 #define DEFAULT_OUTPUT_DEVNAME "System audio output device"
33 #define DEFAULT_INPUT_DEVNAME "System audio capture device"
34 
35 /* The SDL audio driver */
36 typedef struct SDL_AudioDevice SDL_AudioDevice;
37 #define _THIS SDL_AudioDevice *_this
38 
39 /* Audio targets should call this as devices are added to the system (such as
40  a USB headset being plugged in), and should also be called for
41  for every device found during DetectDevices(). */
42 extern void SDL_AddAudioDevice(const int iscapture, const char *name, void *handle);
43 
44 /* Audio targets should call this as devices are removed, so SDL can update
45  its list of available devices. */
46 extern void SDL_RemoveAudioDevice(const int iscapture, void *handle);
47 
48 /* Audio targets should call this if an opened audio device is lost while
49  being used. This can happen due to i/o errors, or a device being unplugged,
50  etc. If the device is totally gone, please also call SDL_RemoveAudioDevice()
51  as appropriate so SDL's list of devices is accurate. */
53 
54 /* This is the size of a packet when using SDL_QueueAudio(). We allocate
55  these as necessary and pool them, under the assumption that we'll
56  eventually end up with a handful that keep recycling, meeting whatever
57  the app needs. We keep packing data tightly as more arrives to avoid
58  wasting space, and if we get a giant block of data, we'll split them
59  into multiple packets behind the scenes. My expectation is that most
60  apps will have 2-3 of these in the pool. 8k should cover most needs, but
61  if this is crippling for some embedded system, we can #ifdef this.
62  The system preallocates enough packets for 2 callbacks' worth of data. */
63 #define SDL_AUDIOBUFFERQUEUE_PACKETLEN (8 * 1024)
64 
65 typedef struct SDL_AudioDriverImpl
66 {
68  int (*OpenDevice) (_THIS, void *handle, const char *devname, int iscapture);
69  void (*ThreadInit) (_THIS); /* Called by audio thread at start */
70  void (*ThreadDeinit) (_THIS); /* Called by audio thread at end */
71  void (*BeginLoopIteration)(_THIS); /* Called by audio thread at top of loop */
75  Uint8 *(*GetDeviceBuf) (_THIS);
76  int (*CaptureFromDevice) (_THIS, void *buffer, int buflen);
78  void (*PrepareToClose) (_THIS); /**< Called between run and draining wait for playback devices */
82  void (*FreeDeviceHandle) (void *handle); /**< SDL is done with handle from SDL_AddAudioDevice() */
84 
85  /* !!! FIXME: add pause(), so we can optimize instead of mixing silence. */
86 
87  /* Some flags to push duplicate code into the core and reduce #ifdefs. */
88  /* !!! FIXME: these should be SDL_bool */
96 
97 
98 typedef struct SDL_AudioDeviceItem
99 {
100  void *handle;
101  char *name;
103  int dupenum;
106 
107 
108 typedef struct SDL_AudioDriver
109 {
110  /* * * */
111  /* The name of this audio driver */
112  const char *name;
113 
114  /* * * */
115  /* The description of this audio driver */
116  const char *desc;
117 
119 
120  /* A mutex for device detection */
129 
130 
131 /* Define the SDL audio driver structure */
133 {
134  /* * * */
135  /* Data common to all devices */
137 
138  /* The device's current audio specification */
140 
141  /* The callback's expected audio specification (converted vs device's spec). */
143 
144  /* Stream that converts and resamples. NULL if not needed. */
145  SDL_AudioStream *stream;
146 
147  /* Current state flags */
148  SDL_atomic_t shutdown; /* true if we are signaling the play thread to end. */
149  SDL_atomic_t enabled; /* true if device is functioning and connected. */
152 
153  /* Scratch buffer used in the bridge between SDL and the user callback. */
155 
156  /* Size, in bytes, of work_buffer. */
158 
159  /* A mutex for locking the mixing buffers */
161 
162  /* A thread to feed the audio device */
165 
166  /* Queued buffers (if app not using callback). */
168 
169  /* * * */
170  /* Data private to this driver */
172 
173  void *handle;
174 };
175 #undef _THIS
176 
177 typedef struct AudioBootStrap
178 {
179  const char *name;
180  const char *desc;
181  int (*init) (SDL_AudioDriverImpl * impl);
182  int demand_only; /* 1==request explicitly, or it won't be available. */
184 
185 /* Not all of these are available in a given build. Use #ifdefs, etc. */
210 
211 #endif /* SDL_sysaudio_h_ */
212 
213 /* vi: set ts=4 sw=4 expandtab: */
SDL_AudioDevice::paused
SDL_atomic_t paused
Definition: SDL_sysaudio.h:150
Uint8
uint8_t Uint8
Definition: SDL_stdinc.h:179
SDL_audio_c.h
SDL_AudioDriverImpl::HasCaptureSupport
int HasCaptureSupport
Definition: SDL_sysaudio.h:91
SDL_AudioDevice::shutdown
SDL_atomic_t shutdown
Definition: SDL_sysaudio.h:148
SDL_AudioDriverImpl::UnlockDevice
void(* UnlockDevice)(_THIS)
Definition: SDL_sysaudio.h:81
SDL_AudioDriverImpl::ProvidesOwnCallbackThread
int ProvidesOwnCallbackThread
Definition: SDL_sysaudio.h:89
SDL_AudioDevice::handle
void * handle
Definition: SDL_sysaudio.h:173
ESD_bootstrap
AudioBootStrap ESD_bootstrap
SDL_AudioDriverImpl::FlushCapture
void(* FlushCapture)(_THIS)
Definition: SDL_sysaudio.h:77
handle
EGLImageKHR EGLint EGLint * handle
Definition: eglext.h:937
PSPAUDIO_bootstrap
AudioBootStrap PSPAUDIO_bootstrap
WINMM_bootstrap
AudioBootStrap WINMM_bootstrap
SDL_AudioDevice::threadid
SDL_threadID threadid
Definition: SDL_sysaudio.h:164
SDL_AudioDriver::captureDevicesRemoved
SDL_bool captureDevicesRemoved
Definition: SDL_sysaudio.h:122
SDL_mutex
Definition: SDL_sysmutex.c:30
SDL_AudioDriverImpl::SkipMixerLock
int SkipMixerLock
Definition: SDL_sysaudio.h:90
SDL_AudioDeviceItem::dupenum
int dupenum
Definition: SDL_sysaudio.h:103
SDL_AudioDriverImpl::GetPendingBytes
int(* GetPendingBytes)(_THIS)
Definition: SDL_sysaudio.h:74
SDL_AudioDeviceItem::next
struct SDL_AudioDeviceItem * next
Definition: SDL_sysaudio.h:104
SDL_AudioDriverImpl::OpenDevice
int(* OpenDevice)(_THIS, void *handle, const char *devname, int iscapture)
Definition: SDL_sysaudio.h:68
SDL_AudioDevice::hidden
struct SDL_PrivateAudioData * hidden
Definition: SDL_sysaudio.h:171
SDL_AudioDevice::id
SDL_AudioDeviceID id
Definition: SDL_sysaudio.h:136
SDL_AudioSpec
Definition: SDL_audio.h:179
SDL_AudioDriverImpl::PrepareToClose
void(* PrepareToClose)(_THIS)
Definition: SDL_sysaudio.h:78
Uint32
uint32_t Uint32
Definition: SDL_stdinc.h:203
DISKAUDIO_bootstrap
AudioBootStrap DISKAUDIO_bootstrap
ARTS_bootstrap
AudioBootStrap ARTS_bootstrap
SDL_AudioDevice::iscapture
SDL_bool iscapture
Definition: SDL_sysaudio.h:151
SDL_AudioDriver::name
const char * name
Definition: SDL_sysaudio.h:112
SDL_Thread
Definition: SDL_thread_c.h:55
QSAAUDIO_bootstrap
AudioBootStrap QSAAUDIO_bootstrap
SUNAUDIO_bootstrap
AudioBootStrap SUNAUDIO_bootstrap
SDL_AudioDriver::desc
const char * desc
Definition: SDL_sysaudio.h:116
SDL_AudioDriver::outputDeviceCount
int outputDeviceCount
Definition: SDL_sysaudio.h:124
SDL_AudioDriverImpl::FreeDeviceHandle
void(* FreeDeviceHandle)(void *handle)
Definition: SDL_sysaudio.h:82
AudioBootStrap
Definition: SDL_sysaudio.h:178
DUMMYAUDIO_bootstrap
AudioBootStrap DUMMYAUDIO_bootstrap
Definition: SDL_dummyaudio.c:61
SDL_AudioDevice::stream
SDL_AudioStream * stream
Definition: SDL_sysaudio.h:145
SDL_AudioDevice::spec
SDL_AudioSpec spec
Definition: SDL_sysaudio.h:139
COREAUDIO_bootstrap
AudioBootStrap COREAUDIO_bootstrap
SDL_AudioDriver::impl
SDL_AudioDriverImpl impl
Definition: SDL_sysaudio.h:118
SDL_PrivateAudioData
Definition: SDL_alsa_audio.h:34
SDL_AudioDriverImpl::WaitDevice
void(* WaitDevice)(_THIS)
Definition: SDL_sysaudio.h:72
EMSCRIPTENAUDIO_bootstrap
AudioBootStrap EMSCRIPTENAUDIO_bootstrap
PAUDIO_bootstrap
AudioBootStrap PAUDIO_bootstrap
buffer
GLuint buffer
Definition: SDL_opengl_glext.h:533
SDL_AudioDeviceItem::name
char * name
Definition: SDL_sysaudio.h:101
SDL_thread.h
AudioBootStrap::init
int(* init)(SDL_AudioDriverImpl *impl)
Definition: SDL_sysaudio.h:181
SNDIO_bootstrap
AudioBootStrap SNDIO_bootstrap
SDL_AddAudioDevice
void SDL_AddAudioDevice(const int iscapture, const char *name, void *handle)
Definition: SDL_audio.c:473
SDL_AudioDriverImpl
Definition: SDL_sysaudio.h:66
DSOUND_bootstrap
AudioBootStrap DSOUND_bootstrap
SDL_AudioDeviceItem::original_name
char * original_name
Definition: SDL_sysaudio.h:102
SDL_RemoveAudioDevice
void SDL_RemoveAudioDevice(const int iscapture, void *handle)
Definition: SDL_audio.c:535
SDL_mutex.h
SDL_AudioDriverImpl::OnlyHasDefaultOutputDevice
int OnlyHasDefaultOutputDevice
Definition: SDL_sysaudio.h:92
name
GLuint const GLchar * name
Definition: SDL_opengl_glext.h:660
NACLAUDIO_bootstrap
AudioBootStrap NACLAUDIO_bootstrap
SDL_AudioDriverImpl::LockDevice
void(* LockDevice)(_THIS)
Definition: SDL_sysaudio.h:80
SDL_AudioDriverImpl::ThreadDeinit
void(* ThreadDeinit)(_THIS)
Definition: SDL_sysaudio.h:70
NAS_bootstrap
AudioBootStrap NAS_bootstrap
SDL_AudioDevice::enabled
SDL_atomic_t enabled
Definition: SDL_sysaudio.h:149
SDL_AudioDriverImpl::DetectDevices
void(* DetectDevices)(void)
Definition: SDL_sysaudio.h:67
WASAPI_bootstrap
AudioBootStrap WASAPI_bootstrap
SDL_AudioDriverImpl::PlayDevice
void(* PlayDevice)(_THIS)
Definition: SDL_sysaudio.h:73
SDL_AudioDeviceItem
Definition: SDL_sysaudio.h:99
ALSA_bootstrap
AudioBootStrap ALSA_bootstrap
PULSEAUDIO_bootstrap
AudioBootStrap PULSEAUDIO_bootstrap
SDL_AudioDriverImpl::BeginLoopIteration
void(* BeginLoopIteration)(_THIS)
Definition: SDL_sysaudio.h:71
SDL_AudioDriverImpl::OnlyHasDefaultCaptureDevice
int OnlyHasDefaultCaptureDevice
Definition: SDL_sysaudio.h:93
SDL_AudioDriverImpl::AllowsArbitraryDeviceNames
int AllowsArbitraryDeviceNames
Definition: SDL_sysaudio.h:94
SDL_AudioDeviceItem::handle
void * handle
Definition: SDL_sysaudio.h:100
SDL_AudioDriverImpl::ThreadInit
void(* ThreadInit)(_THIS)
Definition: SDL_sysaudio.h:69
SDL_AudioDriver::inputDeviceCount
int inputDeviceCount
Definition: SDL_sysaudio.h:125
_THIS
#define _THIS
Definition: SDL_sysaudio.h:37
SDL_AudioDriverImpl::CaptureFromDevice
int(* CaptureFromDevice)(_THIS, void *buffer, int buflen)
Definition: SDL_sysaudio.h:76
FUSIONSOUND_bootstrap
AudioBootStrap FUSIONSOUND_bootstrap
SDL_threadID
unsigned long SDL_threadID
Definition: SDL_thread.h:49
AudioBootStrap::desc
const char * desc
Definition: SDL_sysaudio.h:180
SDL_AudioDevice::thread
SDL_Thread * thread
Definition: SDL_sysaudio.h:163
HAIKUAUDIO_bootstrap
AudioBootStrap HAIKUAUDIO_bootstrap
SDL_atomic_t
A type representing an atomic integer value. It is a struct so people don't accidentally use numeric ...
Definition: SDL_atomic.h:198
SDL_AudioDevice::buffer_queue
SDL_DataQueue * buffer_queue
Definition: SDL_sysaudio.h:167
SDL_AudioDevice::callbackspec
SDL_AudioSpec callbackspec
Definition: SDL_sysaudio.h:142
SDL_AudioDeviceID
Uint32 SDL_AudioDeviceID
Definition: SDL_audio.h:330
SDL_AudioDriver::inputDevices
SDL_AudioDeviceItem * inputDevices
Definition: SDL_sysaudio.h:127
AudioBootStrap::name
const char * name
Definition: SDL_sysaudio.h:179
SDL_AudioDevice::work_buffer
Uint8 * work_buffer
Definition: SDL_sysaudio.h:154
SDL_AudioDriver
Definition: SDL_sysaudio.h:109
ANDROIDAUDIO_bootstrap
AudioBootStrap ANDROIDAUDIO_bootstrap
DSP_bootstrap
AudioBootStrap DSP_bootstrap
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:162
SDL_AudioDriver::outputDevicesRemoved
SDL_bool outputDevicesRemoved
Definition: SDL_sysaudio.h:123
NETBSDAUDIO_bootstrap
AudioBootStrap NETBSDAUDIO_bootstrap
void
SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 void
Definition: SDL_dynapi_procs.h:89
JACK_bootstrap
AudioBootStrap JACK_bootstrap
SDL_DataQueue
Definition: SDL_dataqueue.c:36
SDL_AudioDevice::mixer_lock
SDL_mutex * mixer_lock
Definition: SDL_sysaudio.h:160
device
static SDL_AudioDeviceID device
Definition: loopwave.c:37
SDL_AudioDriver::outputDevices
SDL_AudioDeviceItem * outputDevices
Definition: SDL_sysaudio.h:126
SDL_AudioDriverImpl::CloseDevice
void(* CloseDevice)(_THIS)
Definition: SDL_sysaudio.h:79
SDL_AudioDevice::work_buffer_len
Uint32 work_buffer_len
Definition: SDL_sysaudio.h:157
SDL_AudioDevice
Definition: SDL_sysaudio.h:133
SDL_AudioDriver::detectionLock
SDL_mutex * detectionLock
Definition: SDL_sysaudio.h:121
SDL_AudioDriverImpl::Deinitialize
void(* Deinitialize)(void)
Definition: SDL_sysaudio.h:83
SDL_OpenedAudioDeviceDisconnected
void SDL_OpenedAudioDeviceDisconnected(SDL_AudioDevice *device)
Definition: SDL_audio.c:490
AudioBootStrap::demand_only
int demand_only
Definition: SDL_sysaudio.h:182