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