src/audio/SDL_sysaudio.h
author Ryan C. Gordon <icculus@icculus.org>
Tue, 26 May 2015 21:19:23 -0400
changeset 9649 d7762e30ba24
parent 9619 b94b6d0bff0f
child 9998 f67cf37e9cd4
permissions -rw-r--r--
Stack hint should look for 0, not -1, and not care about environment variables.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2015 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 /* The SDL audio driver */
    30 typedef struct SDL_AudioDevice SDL_AudioDevice;
    31 #define _THIS   SDL_AudioDevice *_this
    32 
    33 /* Audio targets should call this as devices are added to the system (such as
    34    a USB headset being plugged in), and should also be called for
    35    for every device found during DetectDevices(). */
    36 extern void SDL_AddAudioDevice(const int iscapture, const char *name, void *handle);
    37 
    38 /* Audio targets should call this as devices are removed, so SDL can update
    39    its list of available devices. */
    40 extern void SDL_RemoveAudioDevice(const int iscapture, void *handle);
    41 
    42 /* Audio targets should call this if an opened audio device is lost while
    43    being used. This can happen due to i/o errors, or a device being unplugged,
    44    etc. If the device is totally gone, please also call SDL_RemoveAudioDevice()
    45    as appropriate so SDL's list of devices is accurate. */
    46 extern void SDL_OpenedAudioDeviceDisconnected(SDL_AudioDevice *device);
    47 
    48 
    49 /* This is the size of a packet when using SDL_QueueAudio(). We allocate
    50    these as necessary and pool them, under the assumption that we'll
    51    eventually end up with a handful that keep recycling, meeting whatever
    52    the app needs. We keep packing data tightly as more arrives to avoid
    53    wasting space, and if we get a giant block of data, we'll split them
    54    into multiple packets behind the scenes. My expectation is that most
    55    apps will have 2-3 of these in the pool. 8k should cover most needs, but
    56    if this is crippling for some embedded system, we can #ifdef this.
    57    The system preallocates enough packets for 2 callbacks' worth of data. */
    58 #define SDL_AUDIOBUFFERQUEUE_PACKETLEN (8 * 1024)
    59 
    60 /* Used by apps that queue audio instead of using the callback. */
    61 typedef struct SDL_AudioBufferQueue
    62 {
    63     Uint8 data[SDL_AUDIOBUFFERQUEUE_PACKETLEN];  /* packet data. */
    64     Uint32 datalen;  /* bytes currently in use in this packet. */
    65     Uint32 startpos;  /* bytes currently consumed in this packet. */
    66     struct SDL_AudioBufferQueue *next;  /* next item in linked list. */
    67 } SDL_AudioBufferQueue;
    68 
    69 typedef struct SDL_AudioDriverImpl
    70 {
    71     void (*DetectDevices) (void);
    72     int (*OpenDevice) (_THIS, void *handle, const char *devname, int iscapture);
    73     void (*ThreadInit) (_THIS); /* Called by audio thread at start */
    74     void (*WaitDevice) (_THIS);
    75     void (*PlayDevice) (_THIS);
    76     int (*GetPendingBytes) (_THIS);
    77     Uint8 *(*GetDeviceBuf) (_THIS);
    78     void (*WaitDone) (_THIS);
    79     void (*CloseDevice) (_THIS);
    80     void (*LockDevice) (_THIS);
    81     void (*UnlockDevice) (_THIS);
    82     void (*FreeDeviceHandle) (void *handle);  /**< SDL is done with handle from SDL_AddAudioDevice() */
    83     void (*Deinitialize) (void);
    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 */
    89     int ProvidesOwnCallbackThread;
    90     int SkipMixerLock;  /* !!! FIXME: do we need this anymore? */
    91     int HasCaptureSupport;
    92     int OnlyHasDefaultOutputDevice;
    93     int OnlyHasDefaultInputDevice;
    94     int AllowsArbitraryDeviceNames;
    95 } SDL_AudioDriverImpl;
    96 
    97 
    98 typedef struct SDL_AudioDeviceItem
    99 {
   100     void *handle;
   101     struct SDL_AudioDeviceItem *next;
   102     #if (defined(__GNUC__) && (__GNUC__ <= 2))
   103     char name[1];  /* actually variable length. */
   104     #else
   105     char name[];
   106     #endif
   107 } SDL_AudioDeviceItem;
   108 
   109 
   110 typedef struct SDL_AudioDriver
   111 {
   112     /* * * */
   113     /* The name of this audio driver */
   114     const char *name;
   115 
   116     /* * * */
   117     /* The description of this audio driver */
   118     const char *desc;
   119 
   120     SDL_AudioDriverImpl impl;
   121 
   122     /* A mutex for device detection */
   123     SDL_mutex *detectionLock;
   124     SDL_bool captureDevicesRemoved;
   125     SDL_bool outputDevicesRemoved;
   126     int outputDeviceCount;
   127     int inputDeviceCount;
   128     SDL_AudioDeviceItem *outputDevices;
   129     SDL_AudioDeviceItem *inputDevices;
   130 } SDL_AudioDriver;
   131 
   132 
   133 /* Streamer */
   134 typedef struct
   135 {
   136     Uint8 *buffer;
   137     int max_len;                /* the maximum length in bytes */
   138     int read_pos, write_pos;    /* the position of the write and read heads in bytes */
   139 } SDL_AudioStreamer;
   140 
   141 
   142 /* Define the SDL audio driver structure */
   143 struct SDL_AudioDevice
   144 {
   145     /* * * */
   146     /* Data common to all devices */
   147     SDL_AudioDeviceID id;
   148 
   149     /* The current audio specification (shared with audio thread) */
   150     SDL_AudioSpec spec;
   151 
   152     /* An audio conversion block for audio format emulation */
   153     SDL_AudioCVT convert;
   154 
   155     /* The streamer, if sample rate conversion necessitates it */
   156     int use_streamer;
   157     SDL_AudioStreamer streamer;
   158 
   159     /* Current state flags */
   160     /* !!! FIXME: should be SDL_bool */
   161     int iscapture;
   162     int enabled;  /* true if device is functioning and connected. */
   163     int shutdown; /* true if we are signaling the play thread to end. */
   164     int paused;
   165     int opened;
   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 #undef _THIS
   188 
   189 typedef struct AudioBootStrap
   190 {
   191     const char *name;
   192     const char *desc;
   193     int (*init) (SDL_AudioDriverImpl * impl);
   194     int demand_only;  /* 1==request explicitly, or it won't be available. */
   195 } AudioBootStrap;
   196 
   197 #endif /* _SDL_sysaudio_h */
   198 
   199 /* vi: set ts=4 sw=4 expandtab: */