src/audio/SDL_sysaudio.h
author Ryan C. Gordon
Mon, 16 Mar 2015 02:11:39 -0400
changeset 9393 ed79a66e57e5
parent 9391 3a54985e999e
child 9394 bb28e5281770
permissions -rw-r--r--
Initial work on audio device hotplug support.

This fills in the core pieces and fully implements it for Mac OS X.

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