src/audio/SDL_sysaudio.h
author Ryan C. Gordon
Tue, 22 Jul 2014 21:41:49 -0400
changeset 9012 aa058c87737b
parent 8149 681eb46b8ac4
child 9031 e963a13a720c
permissions -rw-r--r--
Added audio device buffer queueing API.
     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     Uint8 *(*GetDeviceBuf) (_THIS);
    64     void (*WaitDone) (_THIS);
    65     void (*CloseDevice) (_THIS);
    66     void (*LockDevice) (_THIS);
    67     void (*UnlockDevice) (_THIS);
    68     void (*Deinitialize) (void);
    69 
    70     /* !!! FIXME: add pause(), so we can optimize instead of mixing silence. */
    71 
    72     /* Some flags to push duplicate code into the core and reduce #ifdefs. */
    73     int ProvidesOwnCallbackThread;
    74     int SkipMixerLock;  /* !!! FIXME: do we need this anymore? */
    75     int HasCaptureSupport;
    76     int OnlyHasDefaultOutputDevice;
    77     int OnlyHasDefaultInputDevice;
    78 } SDL_AudioDriverImpl;
    79 
    80 
    81 typedef struct SDL_AudioDriver
    82 {
    83     /* * * */
    84     /* The name of this audio driver */
    85     const char *name;
    86 
    87     /* * * */
    88     /* The description of this audio driver */
    89     const char *desc;
    90 
    91     SDL_AudioDriverImpl impl;
    92 
    93     char **outputDevices;
    94     int outputDeviceCount;
    95 
    96     char **inputDevices;
    97     int inputDeviceCount;
    98 } SDL_AudioDriver;
    99 
   100 
   101 /* Streamer */
   102 typedef struct
   103 {
   104     Uint8 *buffer;
   105     int max_len;                /* the maximum length in bytes */
   106     int read_pos, write_pos;    /* the position of the write and read heads in bytes */
   107 } SDL_AudioStreamer;
   108 
   109 
   110 /* Define the SDL audio driver structure */
   111 struct SDL_AudioDevice
   112 {
   113     /* * * */
   114     /* Data common to all devices */
   115 
   116     /* The current audio specification (shared with audio thread) */
   117     SDL_AudioSpec spec;
   118 
   119     /* An audio conversion block for audio format emulation */
   120     SDL_AudioCVT convert;
   121 
   122     /* The streamer, if sample rate conversion necessitates it */
   123     int use_streamer;
   124     SDL_AudioStreamer streamer;
   125 
   126     /* Current state flags */
   127     int iscapture;
   128     int enabled;
   129     int paused;
   130     int opened;
   131 
   132     /* Fake audio buffer for when the audio hardware is busy */
   133     Uint8 *fake_stream;
   134 
   135     /* A semaphore for locking the mixing buffers */
   136     SDL_mutex *mixer_lock;
   137 
   138     /* A thread to feed the audio device */
   139     SDL_Thread *thread;
   140     SDL_threadID threadid;
   141 
   142     /* Queued buffers (if app not using callback). */
   143     SDL_AudioBufferQueue *buffer_queue_head; /* device fed from here. */
   144     SDL_AudioBufferQueue *buffer_queue_tail; /* queue fills to here. */
   145     SDL_AudioBufferQueue *buffer_queue_pool; /* these are unused packets. */
   146     Uint32 queued_bytes;  /* number of bytes of audio data in the queue. */
   147 
   148     /* * * */
   149     /* Data private to this driver */
   150     struct SDL_PrivateAudioData *hidden;
   151 };
   152 #undef _THIS
   153 
   154 typedef struct AudioBootStrap
   155 {
   156     const char *name;
   157     const char *desc;
   158     int (*init) (SDL_AudioDriverImpl * impl);
   159     int demand_only;  /* 1==request explicitly, or it won't be available. */
   160 } AudioBootStrap;
   161 
   162 #endif /* _SDL_sysaudio_h */
   163 
   164 /* vi: set ts=4 sw=4 expandtab: */