src/audio/SDL_sysaudio.h
author Ryan C. Gordon
Wed, 18 Jan 2017 02:11:56 -0500
changeset 10817 efc103e60c5b
parent 10765 61312c8c59fe
child 10882 3f210b3522a3
permissions -rw-r--r--
audio: Several fixes to "simple" resampler (thanks, Vitaly!).

Fixes Bugzilla #3551.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2017 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. */
    52 extern void SDL_OpenedAudioDeviceDisconnected(SDL_AudioDevice *device);
    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 {
    67     void (*DetectDevices) (void);
    68     int (*OpenDevice) (_THIS, void *handle, const char *devname, int iscapture);
    69     void (*ThreadInit) (_THIS); /* Called by audio thread at start */
    70     void (*WaitDevice) (_THIS);
    71     void (*PlayDevice) (_THIS);
    72     int (*GetPendingBytes) (_THIS);
    73     Uint8 *(*GetDeviceBuf) (_THIS);
    74     int (*CaptureFromDevice) (_THIS, void *buffer, int buflen);
    75     void (*FlushCapture) (_THIS);
    76     void (*PrepareToClose) (_THIS);  /**< Called between run and draining wait for playback devices */
    77     void (*CloseDevice) (_THIS);
    78     void (*LockDevice) (_THIS);
    79     void (*UnlockDevice) (_THIS);
    80     void (*FreeDeviceHandle) (void *handle);  /**< SDL is done with handle from SDL_AddAudioDevice() */
    81     void (*Deinitialize) (void);
    82 
    83     /* !!! FIXME: add pause(), so we can optimize instead of mixing silence. */
    84 
    85     /* Some flags to push duplicate code into the core and reduce #ifdefs. */
    86     /* !!! FIXME: these should be SDL_bool */
    87     int ProvidesOwnCallbackThread;
    88     int SkipMixerLock;
    89     int HasCaptureSupport;
    90     int OnlyHasDefaultOutputDevice;
    91     int OnlyHasDefaultCaptureDevice;
    92     int AllowsArbitraryDeviceNames;
    93 } SDL_AudioDriverImpl;
    94 
    95 
    96 typedef struct SDL_AudioDeviceItem
    97 {
    98     void *handle;
    99     struct SDL_AudioDeviceItem *next;
   100     char name[SDL_VARIABLE_LENGTH_ARRAY];
   101 } SDL_AudioDeviceItem;
   102 
   103 
   104 typedef struct SDL_AudioDriver
   105 {
   106     /* * * */
   107     /* The name of this audio driver */
   108     const char *name;
   109 
   110     /* * * */
   111     /* The description of this audio driver */
   112     const char *desc;
   113 
   114     SDL_AudioDriverImpl impl;
   115 
   116     /* A mutex for device detection */
   117     SDL_mutex *detectionLock;
   118     SDL_bool captureDevicesRemoved;
   119     SDL_bool outputDevicesRemoved;
   120     int outputDeviceCount;
   121     int inputDeviceCount;
   122     SDL_AudioDeviceItem *outputDevices;
   123     SDL_AudioDeviceItem *inputDevices;
   124 } SDL_AudioDriver;
   125 
   126 
   127 /* Define the SDL audio driver structure */
   128 struct SDL_AudioDevice
   129 {
   130     /* * * */
   131     /* Data common to all devices */
   132     SDL_AudioDeviceID id;
   133 
   134     /* The device's current audio specification */
   135     SDL_AudioSpec spec;
   136 
   137     /* The callback's expected audio specification (converted vs device's spec). */
   138     SDL_AudioSpec callbackspec;
   139 
   140     /* Stream that converts and resamples. NULL if not needed. */
   141     SDL_AudioStream *stream;
   142 
   143     /* Current state flags */
   144     SDL_atomic_t shutdown; /* true if we are signaling the play thread to end. */
   145     SDL_atomic_t enabled;  /* true if device is functioning and connected. */
   146     SDL_atomic_t paused;
   147     SDL_bool iscapture;
   148 
   149     /* Scratch buffer used in the bridge between SDL and the user callback. */
   150     Uint8 *work_buffer;
   151 
   152     /* Size, in bytes, of work_buffer. */
   153     Uint32 work_buffer_len;
   154 
   155     /* A mutex for locking the mixing buffers */
   156     SDL_mutex *mixer_lock;
   157 
   158     /* A thread to feed the audio device */
   159     SDL_Thread *thread;
   160     SDL_threadID threadid;
   161 
   162     /* Queued buffers (if app not using callback). */
   163     SDL_DataQueue *buffer_queue;
   164 
   165     /* * * */
   166     /* Data private to this driver */
   167     struct SDL_PrivateAudioData *hidden;
   168 
   169     void *handle;
   170 };
   171 #undef _THIS
   172 
   173 typedef struct AudioBootStrap
   174 {
   175     const char *name;
   176     const char *desc;
   177     int (*init) (SDL_AudioDriverImpl * impl);
   178     int demand_only;  /* 1==request explicitly, or it won't be available. */
   179 } AudioBootStrap;
   180 
   181 #if SDL_AUDIO_DRIVER_PULSEAUDIO
   182 extern AudioBootStrap PULSEAUDIO_bootstrap;
   183 #endif
   184 #if SDL_AUDIO_DRIVER_ALSA
   185 extern AudioBootStrap ALSA_bootstrap;
   186 #endif
   187 #if SDL_AUDIO_DRIVER_SNDIO
   188 extern AudioBootStrap SNDIO_bootstrap;
   189 #endif
   190 #if SDL_AUDIO_DRIVER_BSD
   191 extern AudioBootStrap BSD_AUDIO_bootstrap;
   192 #endif
   193 #if SDL_AUDIO_DRIVER_OSS
   194 extern AudioBootStrap DSP_bootstrap;
   195 #endif
   196 #if SDL_AUDIO_DRIVER_QSA
   197 extern AudioBootStrap QSAAUDIO_bootstrap;
   198 #endif
   199 #if SDL_AUDIO_DRIVER_SUNAUDIO
   200 extern AudioBootStrap SUNAUDIO_bootstrap;
   201 #endif
   202 #if SDL_AUDIO_DRIVER_ARTS
   203 extern AudioBootStrap ARTS_bootstrap;
   204 #endif
   205 #if SDL_AUDIO_DRIVER_ESD
   206 extern AudioBootStrap ESD_bootstrap;
   207 #endif
   208 #if SDL_AUDIO_DRIVER_NACL
   209 extern AudioBootStrap NACLAUDIO_bootstrap;
   210 #endif
   211 #if SDL_AUDIO_DRIVER_NAS
   212 extern AudioBootStrap NAS_bootstrap;
   213 #endif
   214 #if SDL_AUDIO_DRIVER_XAUDIO2
   215 extern AudioBootStrap XAUDIO2_bootstrap;
   216 #endif
   217 #if SDL_AUDIO_DRIVER_DSOUND
   218 extern AudioBootStrap DSOUND_bootstrap;
   219 #endif
   220 #if SDL_AUDIO_DRIVER_WINMM
   221 extern AudioBootStrap WINMM_bootstrap;
   222 #endif
   223 #if SDL_AUDIO_DRIVER_PAUDIO
   224 extern AudioBootStrap PAUDIO_bootstrap;
   225 #endif
   226 #if SDL_AUDIO_DRIVER_HAIKU
   227 extern AudioBootStrap HAIKUAUDIO_bootstrap;
   228 #endif
   229 #if SDL_AUDIO_DRIVER_COREAUDIO
   230 extern AudioBootStrap COREAUDIO_bootstrap;
   231 #endif
   232 #if SDL_AUDIO_DRIVER_DISK
   233 extern AudioBootStrap DISKAUDIO_bootstrap;
   234 #endif
   235 #if SDL_AUDIO_DRIVER_DUMMY
   236 extern AudioBootStrap DUMMYAUDIO_bootstrap;
   237 #endif
   238 #if SDL_AUDIO_DRIVER_FUSIONSOUND
   239 extern AudioBootStrap FUSIONSOUND_bootstrap;
   240 #endif
   241 #if SDL_AUDIO_DRIVER_ANDROID
   242 extern AudioBootStrap ANDROIDAUDIO_bootstrap;
   243 #endif
   244 #if SDL_AUDIO_DRIVER_PSP
   245 extern AudioBootStrap PSPAUDIO_bootstrap;
   246 #endif
   247 #if SDL_AUDIO_DRIVER_EMSCRIPTEN
   248 extern AudioBootStrap EMSCRIPTENAUDIO_bootstrap;
   249 #endif
   250 
   251 
   252 
   253 #endif /* SDL_sysaudio_h_ */
   254 
   255 /* vi: set ts=4 sw=4 expandtab: */