src/audio/SDL_sysaudio.h
author Ryan C. Gordon
Wed, 18 Mar 2015 10:09:23 -0400
changeset 9391 3a54985e999e
parent 9031 e963a13a720c
child 9393 ed79a66e57e5
permissions -rw-r--r--
Tweaked a couple comments, added some FIXMEs.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2014 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 /* Used by audio targets during DetectDevices() */
    34 typedef void (*SDL_AddAudioDevice)(const char *name);
    35 
    36 /* This is the size of a packet when using SDL_QueueAudio(). We allocate
    37    these as necessary and pool them, under the assumption that we'll
    38    eventually end up with a handful that keep recycling, meeting whatever
    39    the app needs. We keep packing data tightly as more arrives to avoid
    40    wasting space, and if we get a giant block of data, we'll split them
    41    into multiple packets behind the scenes. My expectation is that most
    42    apps will have 2-3 of these in the pool. 8k should cover most needs, but
    43    if this is crippling for some embedded system, we can #ifdef this.
    44    The system preallocates enough packets for 2 callbacks' worth of data. */
    45 #define SDL_AUDIOBUFFERQUEUE_PACKETLEN (8 * 1024)
    46 
    47 /* Used by apps that queue audio instead of using the callback. */
    48 typedef struct SDL_AudioBufferQueue
    49 {
    50     Uint8 data[SDL_AUDIOBUFFERQUEUE_PACKETLEN];  /* packet data. */
    51     Uint32 datalen;  /* bytes currently in use in this packet. */
    52     Uint32 startpos;  /* bytes currently consumed in this packet. */
    53     struct SDL_AudioBufferQueue *next;  /* next item in linked list. */
    54 } SDL_AudioBufferQueue;
    55 
    56 typedef struct SDL_AudioDriverImpl
    57 {
    58     void (*DetectDevices) (int iscapture, SDL_AddAudioDevice addfn);
    59     int (*OpenDevice) (_THIS, const char *devname, int iscapture);
    60     void (*ThreadInit) (_THIS); /* Called by audio thread at start */
    61     void (*WaitDevice) (_THIS);
    62     void (*PlayDevice) (_THIS);
    63     int (*GetPendingBytes) (_THIS);
    64     Uint8 *(*GetDeviceBuf) (_THIS);
    65     void (*WaitDone) (_THIS);
    66     void (*CloseDevice) (_THIS);
    67     void (*LockDevice) (_THIS);
    68     void (*UnlockDevice) (_THIS);
    69     void (*Deinitialize) (void);
    70 
    71     /* !!! FIXME: add pause(), so we can optimize instead of mixing silence. */
    72 
    73     /* Some flags to push duplicate code into the core and reduce #ifdefs. */
    74     /* !!! FIXME: these should be SDL_bool */
    75     int ProvidesOwnCallbackThread;
    76     int SkipMixerLock;  /* !!! FIXME: do we need this anymore? */
    77     int HasCaptureSupport;
    78     int OnlyHasDefaultOutputDevice;
    79     int OnlyHasDefaultInputDevice;
    80 } SDL_AudioDriverImpl;
    81 
    82 
    83 typedef struct SDL_AudioDriver
    84 {
    85     /* * * */
    86     /* The name of this audio driver */
    87     const char *name;
    88 
    89     /* * * */
    90     /* The description of this audio driver */
    91     const char *desc;
    92 
    93     SDL_AudioDriverImpl impl;
    94 
    95     char **outputDevices;
    96     int outputDeviceCount;
    97 
    98     char **inputDevices;
    99     int inputDeviceCount;
   100 } SDL_AudioDriver;
   101 
   102 
   103 /* Streamer */
   104 typedef struct
   105 {
   106     Uint8 *buffer;
   107     int max_len;                /* the maximum length in bytes */
   108     int read_pos, write_pos;    /* the position of the write and read heads in bytes */
   109 } SDL_AudioStreamer;
   110 
   111 
   112 /* Define the SDL audio driver structure */
   113 struct SDL_AudioDevice
   114 {
   115     /* * * */
   116     /* Data common to all devices */
   117 
   118     /* The current audio specification (shared with audio thread) */
   119     SDL_AudioSpec spec;
   120 
   121     /* An audio conversion block for audio format emulation */
   122     SDL_AudioCVT convert;
   123 
   124     /* The streamer, if sample rate conversion necessitates it */
   125     int use_streamer;
   126     SDL_AudioStreamer streamer;
   127 
   128     /* Current state flags */
   129     /* !!! FIXME: should be SDL_bool */
   130     int iscapture;
   131     int enabled;
   132     int paused;
   133     int opened;
   134 
   135     /* Fake audio buffer for when the audio hardware is busy */
   136     Uint8 *fake_stream;
   137 
   138     /* A mutex for locking the mixing buffers */
   139     SDL_mutex *mixer_lock;
   140 
   141     /* A thread to feed the audio device */
   142     SDL_Thread *thread;
   143     SDL_threadID threadid;
   144 
   145     /* Queued buffers (if app not using callback). */
   146     SDL_AudioBufferQueue *buffer_queue_head; /* device fed from here. */
   147     SDL_AudioBufferQueue *buffer_queue_tail; /* queue fills to here. */
   148     SDL_AudioBufferQueue *buffer_queue_pool; /* these are unused packets. */
   149     Uint32 queued_bytes;  /* number of bytes of audio data in the queue. */
   150 
   151     /* * * */
   152     /* Data private to this driver */
   153     struct SDL_PrivateAudioData *hidden;
   154 };
   155 #undef _THIS
   156 
   157 typedef struct AudioBootStrap
   158 {
   159     const char *name;
   160     const char *desc;
   161     int (*init) (SDL_AudioDriverImpl * impl);
   162     int demand_only;  /* 1==request explicitly, or it won't be available. */
   163 } AudioBootStrap;
   164 
   165 #endif /* _SDL_sysaudio_h */
   166 
   167 /* vi: set ts=4 sw=4 expandtab: */