src/audio/SDL_sysaudio.h
author Ryan C. Gordon
Tue, 06 Dec 2016 02:20:58 -0500
changeset 10680 fe759a74b018
parent 10638 d11daa346140
child 10681 34b25c97b17f
permissions -rw-r--r--
Added SDL_VARIABLE_LENGTH_ARRAY so this #ifdef is localized to one place.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2016 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 
    29 /* !!! FIXME: These are wordy and unlocalized... */
    30 #define DEFAULT_OUTPUT_DEVNAME "System audio output device"
    31 #define DEFAULT_INPUT_DEVNAME "System audio capture device"
    32 
    33 /* The SDL audio driver */
    34 typedef struct SDL_AudioDevice SDL_AudioDevice;
    35 #define _THIS   SDL_AudioDevice *_this
    36 
    37 /* Audio targets should call this as devices are added to the system (such as
    38    a USB headset being plugged in), and should also be called for
    39    for every device found during DetectDevices(). */
    40 extern void SDL_AddAudioDevice(const int iscapture, const char *name, void *handle);
    41 
    42 /* Audio targets should call this as devices are removed, so SDL can update
    43    its list of available devices. */
    44 extern void SDL_RemoveAudioDevice(const int iscapture, void *handle);
    45 
    46 /* Audio targets should call this if an opened audio device is lost while
    47    being used. This can happen due to i/o errors, or a device being unplugged,
    48    etc. If the device is totally gone, please also call SDL_RemoveAudioDevice()
    49    as appropriate so SDL's list of devices is accurate. */
    50 extern void SDL_OpenedAudioDeviceDisconnected(SDL_AudioDevice *device);
    51 
    52 
    53 /* This is the size of a packet when using SDL_QueueAudio(). We allocate
    54    these as necessary and pool them, under the assumption that we'll
    55    eventually end up with a handful that keep recycling, meeting whatever
    56    the app needs. We keep packing data tightly as more arrives to avoid
    57    wasting space, and if we get a giant block of data, we'll split them
    58    into multiple packets behind the scenes. My expectation is that most
    59    apps will have 2-3 of these in the pool. 8k should cover most needs, but
    60    if this is crippling for some embedded system, we can #ifdef this.
    61    The system preallocates enough packets for 2 callbacks' worth of data. */
    62 #define SDL_AUDIOBUFFERQUEUE_PACKETLEN (8 * 1024)
    63 
    64 /* Used by apps that queue audio instead of using the callback. */
    65 typedef struct SDL_AudioBufferQueue
    66 {
    67     Uint8 data[SDL_AUDIOBUFFERQUEUE_PACKETLEN];  /* packet data. */
    68     Uint32 datalen;  /* bytes currently in use in this packet. */
    69     Uint32 startpos;  /* bytes currently consumed in this packet. */
    70     struct SDL_AudioBufferQueue *next;  /* next item in linked list. */
    71 } SDL_AudioBufferQueue;
    72 
    73 typedef struct SDL_AudioDriverImpl
    74 {
    75     void (*DetectDevices) (void);
    76     int (*OpenDevice) (_THIS, void *handle, const char *devname, int iscapture);
    77     void (*ThreadInit) (_THIS); /* Called by audio thread at start */
    78     void (*WaitDevice) (_THIS);
    79     void (*PlayDevice) (_THIS);
    80     int (*GetPendingBytes) (_THIS);
    81     Uint8 *(*GetDeviceBuf) (_THIS);
    82     int (*CaptureFromDevice) (_THIS, void *buffer, int buflen);
    83     void (*FlushCapture) (_THIS);
    84     void (*PrepareToClose) (_THIS);  /**< Called between run and draining wait for playback devices */
    85     void (*CloseDevice) (_THIS);
    86     void (*LockDevice) (_THIS);
    87     void (*UnlockDevice) (_THIS);
    88     void (*FreeDeviceHandle) (void *handle);  /**< SDL is done with handle from SDL_AddAudioDevice() */
    89     void (*Deinitialize) (void);
    90 
    91     /* !!! FIXME: add pause(), so we can optimize instead of mixing silence. */
    92 
    93     /* Some flags to push duplicate code into the core and reduce #ifdefs. */
    94     /* !!! FIXME: these should be SDL_bool */
    95     int ProvidesOwnCallbackThread;
    96     int SkipMixerLock;
    97     int HasCaptureSupport;
    98     int OnlyHasDefaultOutputDevice;
    99     int OnlyHasDefaultCaptureDevice;
   100     int AllowsArbitraryDeviceNames;
   101 } SDL_AudioDriverImpl;
   102 
   103 
   104 typedef struct SDL_AudioDeviceItem
   105 {
   106     void *handle;
   107     struct SDL_AudioDeviceItem *next;
   108     char name[SDL_VARIABLE_LENGTH_ARRAY];
   109 } SDL_AudioDeviceItem;
   110 
   111 
   112 typedef struct SDL_AudioDriver
   113 {
   114     /* * * */
   115     /* The name of this audio driver */
   116     const char *name;
   117 
   118     /* * * */
   119     /* The description of this audio driver */
   120     const char *desc;
   121 
   122     SDL_AudioDriverImpl impl;
   123 
   124     /* A mutex for device detection */
   125     SDL_mutex *detectionLock;
   126     SDL_bool captureDevicesRemoved;
   127     SDL_bool outputDevicesRemoved;
   128     int outputDeviceCount;
   129     int inputDeviceCount;
   130     SDL_AudioDeviceItem *outputDevices;
   131     SDL_AudioDeviceItem *inputDevices;
   132 } SDL_AudioDriver;
   133 
   134 
   135 /* Streamer */
   136 typedef struct
   137 {
   138     Uint8 *buffer;
   139     int max_len;                /* the maximum length in bytes */
   140     int read_pos, write_pos;    /* the position of the write and read heads in bytes */
   141 } SDL_AudioStreamer;
   142 
   143 
   144 /* Define the SDL audio driver structure */
   145 struct SDL_AudioDevice
   146 {
   147     /* * * */
   148     /* Data common to all devices */
   149     SDL_AudioDeviceID id;
   150 
   151     /* The current audio specification (shared with audio thread) */
   152     SDL_AudioSpec spec;
   153 
   154     /* An audio conversion block for audio format emulation */
   155     SDL_AudioCVT convert;
   156 
   157     /* The streamer, if sample rate conversion necessitates it */
   158     int use_streamer;
   159     SDL_AudioStreamer streamer;
   160 
   161     /* Current state flags */
   162     SDL_atomic_t shutdown; /* true if we are signaling the play thread to end. */
   163     SDL_atomic_t enabled;  /* true if device is functioning and connected. */
   164     SDL_atomic_t paused;
   165     SDL_bool iscapture;
   166 
   167     /* Fake audio buffer for when the audio hardware is busy */
   168     Uint8 *fake_stream;
   169 
   170     /* A mutex for locking the mixing buffers */
   171     SDL_mutex *mixer_lock;
   172 
   173     /* A thread to feed the audio device */
   174     SDL_Thread *thread;
   175     SDL_threadID threadid;
   176 
   177     /* Queued buffers (if app not using callback). */
   178     SDL_AudioBufferQueue *buffer_queue_head; /* device fed from here. */
   179     SDL_AudioBufferQueue *buffer_queue_tail; /* queue fills to here. */
   180     SDL_AudioBufferQueue *buffer_queue_pool; /* these are unused packets. */
   181     Uint32 queued_bytes;  /* number of bytes of audio data in the queue. */
   182 
   183     /* * * */
   184     /* Data private to this driver */
   185     struct SDL_PrivateAudioData *hidden;
   186 
   187     void *handle;
   188 };
   189 #undef _THIS
   190 
   191 typedef struct AudioBootStrap
   192 {
   193     const char *name;
   194     const char *desc;
   195     int (*init) (SDL_AudioDriverImpl * impl);
   196     int demand_only;  /* 1==request explicitly, or it won't be available. */
   197 } AudioBootStrap;
   198 
   199 #if SDL_AUDIO_DRIVER_PULSEAUDIO
   200 extern AudioBootStrap PULSEAUDIO_bootstrap;
   201 #endif
   202 #if SDL_AUDIO_DRIVER_ALSA
   203 extern AudioBootStrap ALSA_bootstrap;
   204 #endif
   205 #if SDL_AUDIO_DRIVER_SNDIO
   206 extern AudioBootStrap SNDIO_bootstrap;
   207 #endif
   208 #if SDL_AUDIO_DRIVER_BSD
   209 extern AudioBootStrap BSD_AUDIO_bootstrap;
   210 #endif
   211 #if SDL_AUDIO_DRIVER_OSS
   212 extern AudioBootStrap DSP_bootstrap;
   213 #endif
   214 #if SDL_AUDIO_DRIVER_QSA
   215 extern AudioBootStrap QSAAUDIO_bootstrap;
   216 #endif
   217 #if SDL_AUDIO_DRIVER_SUNAUDIO
   218 extern AudioBootStrap SUNAUDIO_bootstrap;
   219 #endif
   220 #if SDL_AUDIO_DRIVER_ARTS
   221 extern AudioBootStrap ARTS_bootstrap;
   222 #endif
   223 #if SDL_AUDIO_DRIVER_ESD
   224 extern AudioBootStrap ESD_bootstrap;
   225 #endif
   226 #if SDL_AUDIO_DRIVER_NACL
   227 extern AudioBootStrap NACLAUDIO_bootstrap;
   228 #endif
   229 #if SDL_AUDIO_DRIVER_NAS
   230 extern AudioBootStrap NAS_bootstrap;
   231 #endif
   232 #if SDL_AUDIO_DRIVER_XAUDIO2
   233 extern AudioBootStrap XAUDIO2_bootstrap;
   234 #endif
   235 #if SDL_AUDIO_DRIVER_DSOUND
   236 extern AudioBootStrap DSOUND_bootstrap;
   237 #endif
   238 #if SDL_AUDIO_DRIVER_WINMM
   239 extern AudioBootStrap WINMM_bootstrap;
   240 #endif
   241 #if SDL_AUDIO_DRIVER_PAUDIO
   242 extern AudioBootStrap PAUDIO_bootstrap;
   243 #endif
   244 #if SDL_AUDIO_DRIVER_HAIKU
   245 extern AudioBootStrap HAIKUAUDIO_bootstrap;
   246 #endif
   247 #if SDL_AUDIO_DRIVER_COREAUDIO
   248 extern AudioBootStrap COREAUDIO_bootstrap;
   249 #endif
   250 #if SDL_AUDIO_DRIVER_DISK
   251 extern AudioBootStrap DISKAUDIO_bootstrap;
   252 #endif
   253 #if SDL_AUDIO_DRIVER_DUMMY
   254 extern AudioBootStrap DUMMYAUDIO_bootstrap;
   255 #endif
   256 #if SDL_AUDIO_DRIVER_FUSIONSOUND
   257 extern AudioBootStrap FUSIONSOUND_bootstrap;
   258 #endif
   259 #if SDL_AUDIO_DRIVER_ANDROID
   260 extern AudioBootStrap ANDROIDAUDIO_bootstrap;
   261 #endif
   262 #if SDL_AUDIO_DRIVER_PSP
   263 extern AudioBootStrap PSPAUDIO_bootstrap;
   264 #endif
   265 #if SDL_AUDIO_DRIVER_EMSCRIPTEN
   266 extern AudioBootStrap EMSCRIPTENAUDIO_bootstrap;
   267 #endif
   268 
   269 
   270 
   271 #endif /* SDL_sysaudio_h_ */
   272 
   273 /* vi: set ts=4 sw=4 expandtab: */