src/audio/SDL_sysaudio.h
author Ryan C. Gordon
Tue, 06 Dec 2016 02:23:54 -0500
changeset 10681 34b25c97b17f
parent 10680 fe759a74b018
child 10737 3406a0f8b041
permissions -rw-r--r--
Refactored the audio queueing code to a generic SDL_DataQueue interface.

This is not a public API (at the moment), but we will be needing this for
other internal things soon.
     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 #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 /* Audio targets should call this as devices are added to the system (such as
    39    a USB headset being plugged in), and should also be called for
    40    for every device found during DetectDevices(). */
    41 extern void SDL_AddAudioDevice(const int iscapture, const char *name, void *handle);
    42 
    43 /* Audio targets should call this as devices are removed, so SDL can update
    44    its list of available devices. */
    45 extern void SDL_RemoveAudioDevice(const int iscapture, void *handle);
    46 
    47 /* Audio targets should call this if an opened audio device is lost while
    48    being used. This can happen due to i/o errors, or a device being unplugged,
    49    etc. If the device is totally gone, please also call SDL_RemoveAudioDevice()
    50    as appropriate so SDL's list of devices is accurate. */
    51 extern void SDL_OpenedAudioDeviceDisconnected(SDL_AudioDevice *device);
    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 typedef struct SDL_AudioDriverImpl
    65 {
    66     void (*DetectDevices) (void);
    67     int (*OpenDevice) (_THIS, void *handle, const char *devname, int iscapture);
    68     void (*ThreadInit) (_THIS); /* Called by audio thread at start */
    69     void (*WaitDevice) (_THIS);
    70     void (*PlayDevice) (_THIS);
    71     int (*GetPendingBytes) (_THIS);
    72     Uint8 *(*GetDeviceBuf) (_THIS);
    73     int (*CaptureFromDevice) (_THIS, void *buffer, int buflen);
    74     void (*FlushCapture) (_THIS);
    75     void (*PrepareToClose) (_THIS);  /**< Called between run and draining wait for playback devices */
    76     void (*CloseDevice) (_THIS);
    77     void (*LockDevice) (_THIS);
    78     void (*UnlockDevice) (_THIS);
    79     void (*FreeDeviceHandle) (void *handle);  /**< SDL is done with handle from SDL_AddAudioDevice() */
    80     void (*Deinitialize) (void);
    81 
    82     /* !!! FIXME: add pause(), so we can optimize instead of mixing silence. */
    83 
    84     /* Some flags to push duplicate code into the core and reduce #ifdefs. */
    85     /* !!! FIXME: these should be SDL_bool */
    86     int ProvidesOwnCallbackThread;
    87     int SkipMixerLock;
    88     int HasCaptureSupport;
    89     int OnlyHasDefaultOutputDevice;
    90     int OnlyHasDefaultCaptureDevice;
    91     int AllowsArbitraryDeviceNames;
    92 } SDL_AudioDriverImpl;
    93 
    94 
    95 typedef struct SDL_AudioDeviceItem
    96 {
    97     void *handle;
    98     struct SDL_AudioDeviceItem *next;
    99     char name[SDL_VARIABLE_LENGTH_ARRAY];
   100 } SDL_AudioDeviceItem;
   101 
   102 
   103 typedef struct SDL_AudioDriver
   104 {
   105     /* * * */
   106     /* The name of this audio driver */
   107     const char *name;
   108 
   109     /* * * */
   110     /* The description of this audio driver */
   111     const char *desc;
   112 
   113     SDL_AudioDriverImpl impl;
   114 
   115     /* A mutex for device detection */
   116     SDL_mutex *detectionLock;
   117     SDL_bool captureDevicesRemoved;
   118     SDL_bool outputDevicesRemoved;
   119     int outputDeviceCount;
   120     int inputDeviceCount;
   121     SDL_AudioDeviceItem *outputDevices;
   122     SDL_AudioDeviceItem *inputDevices;
   123 } SDL_AudioDriver;
   124 
   125 
   126 /* Streamer */
   127 typedef struct
   128 {
   129     Uint8 *buffer;
   130     int max_len;                /* the maximum length in bytes */
   131     int read_pos, write_pos;    /* the position of the write and read heads in bytes */
   132 } SDL_AudioStreamer;
   133 
   134 
   135 /* Define the SDL audio driver structure */
   136 struct SDL_AudioDevice
   137 {
   138     /* * * */
   139     /* Data common to all devices */
   140     SDL_AudioDeviceID id;
   141 
   142     /* The current audio specification (shared with audio thread) */
   143     SDL_AudioSpec spec;
   144 
   145     /* An audio conversion block for audio format emulation */
   146     SDL_AudioCVT convert;
   147 
   148     /* The streamer, if sample rate conversion necessitates it */
   149     int use_streamer;
   150     SDL_AudioStreamer streamer;
   151 
   152     /* Current state flags */
   153     SDL_atomic_t shutdown; /* true if we are signaling the play thread to end. */
   154     SDL_atomic_t enabled;  /* true if device is functioning and connected. */
   155     SDL_atomic_t paused;
   156     SDL_bool iscapture;
   157 
   158     /* Fake audio buffer for when the audio hardware is busy */
   159     Uint8 *fake_stream;
   160 
   161     /* A mutex for locking the mixing buffers */
   162     SDL_mutex *mixer_lock;
   163 
   164     /* A thread to feed the audio device */
   165     SDL_Thread *thread;
   166     SDL_threadID threadid;
   167 
   168     /* Queued buffers (if app not using callback). */
   169     SDL_DataQueue *buffer_queue;
   170 
   171     /* * * */
   172     /* Data private to this driver */
   173     struct SDL_PrivateAudioData *hidden;
   174 
   175     void *handle;
   176 };
   177 #undef _THIS
   178 
   179 typedef struct AudioBootStrap
   180 {
   181     const char *name;
   182     const char *desc;
   183     int (*init) (SDL_AudioDriverImpl * impl);
   184     int demand_only;  /* 1==request explicitly, or it won't be available. */
   185 } AudioBootStrap;
   186 
   187 #if SDL_AUDIO_DRIVER_PULSEAUDIO
   188 extern AudioBootStrap PULSEAUDIO_bootstrap;
   189 #endif
   190 #if SDL_AUDIO_DRIVER_ALSA
   191 extern AudioBootStrap ALSA_bootstrap;
   192 #endif
   193 #if SDL_AUDIO_DRIVER_SNDIO
   194 extern AudioBootStrap SNDIO_bootstrap;
   195 #endif
   196 #if SDL_AUDIO_DRIVER_BSD
   197 extern AudioBootStrap BSD_AUDIO_bootstrap;
   198 #endif
   199 #if SDL_AUDIO_DRIVER_OSS
   200 extern AudioBootStrap DSP_bootstrap;
   201 #endif
   202 #if SDL_AUDIO_DRIVER_QSA
   203 extern AudioBootStrap QSAAUDIO_bootstrap;
   204 #endif
   205 #if SDL_AUDIO_DRIVER_SUNAUDIO
   206 extern AudioBootStrap SUNAUDIO_bootstrap;
   207 #endif
   208 #if SDL_AUDIO_DRIVER_ARTS
   209 extern AudioBootStrap ARTS_bootstrap;
   210 #endif
   211 #if SDL_AUDIO_DRIVER_ESD
   212 extern AudioBootStrap ESD_bootstrap;
   213 #endif
   214 #if SDL_AUDIO_DRIVER_NACL
   215 extern AudioBootStrap NACLAUDIO_bootstrap;
   216 #endif
   217 #if SDL_AUDIO_DRIVER_NAS
   218 extern AudioBootStrap NAS_bootstrap;
   219 #endif
   220 #if SDL_AUDIO_DRIVER_XAUDIO2
   221 extern AudioBootStrap XAUDIO2_bootstrap;
   222 #endif
   223 #if SDL_AUDIO_DRIVER_DSOUND
   224 extern AudioBootStrap DSOUND_bootstrap;
   225 #endif
   226 #if SDL_AUDIO_DRIVER_WINMM
   227 extern AudioBootStrap WINMM_bootstrap;
   228 #endif
   229 #if SDL_AUDIO_DRIVER_PAUDIO
   230 extern AudioBootStrap PAUDIO_bootstrap;
   231 #endif
   232 #if SDL_AUDIO_DRIVER_HAIKU
   233 extern AudioBootStrap HAIKUAUDIO_bootstrap;
   234 #endif
   235 #if SDL_AUDIO_DRIVER_COREAUDIO
   236 extern AudioBootStrap COREAUDIO_bootstrap;
   237 #endif
   238 #if SDL_AUDIO_DRIVER_DISK
   239 extern AudioBootStrap DISKAUDIO_bootstrap;
   240 #endif
   241 #if SDL_AUDIO_DRIVER_DUMMY
   242 extern AudioBootStrap DUMMYAUDIO_bootstrap;
   243 #endif
   244 #if SDL_AUDIO_DRIVER_FUSIONSOUND
   245 extern AudioBootStrap FUSIONSOUND_bootstrap;
   246 #endif
   247 #if SDL_AUDIO_DRIVER_ANDROID
   248 extern AudioBootStrap ANDROIDAUDIO_bootstrap;
   249 #endif
   250 #if SDL_AUDIO_DRIVER_PSP
   251 extern AudioBootStrap PSPAUDIO_bootstrap;
   252 #endif
   253 #if SDL_AUDIO_DRIVER_EMSCRIPTEN
   254 extern AudioBootStrap EMSCRIPTENAUDIO_bootstrap;
   255 #endif
   256 
   257 
   258 
   259 #endif /* SDL_sysaudio_h_ */
   260 
   261 /* vi: set ts=4 sw=4 expandtab: */