src/audio/SDL_sysaudio.h
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sun, 22 Feb 2015 23:21:32 +0100
changeset 9373 679eb3986e37
parent 9031 e963a13a720c
child 9391 3a54985e999e
permissions -rw-r--r--
Emscripten: Fixed out of range joystick device index after joystick disconnect.

After disconnecting a joystick the remaining kept their original device index.
This was not correct because the device index must be a number between 0 and
SDL_NumJoysticks(). It was fixed with ideas from SDL's joystick implementation
for Android. Some range checks were removed as the caller already checks them.
     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     int ProvidesOwnCallbackThread;
    75     int SkipMixerLock;  /* !!! FIXME: do we need this anymore? */
    76     int HasCaptureSupport;
    77     int OnlyHasDefaultOutputDevice;
    78     int OnlyHasDefaultInputDevice;
    79 } SDL_AudioDriverImpl;
    80 
    81 
    82 typedef struct SDL_AudioDriver
    83 {
    84     /* * * */
    85     /* The name of this audio driver */
    86     const char *name;
    87 
    88     /* * * */
    89     /* The description of this audio driver */
    90     const char *desc;
    91 
    92     SDL_AudioDriverImpl impl;
    93 
    94     char **outputDevices;
    95     int outputDeviceCount;
    96 
    97     char **inputDevices;
    98     int inputDeviceCount;
    99 } SDL_AudioDriver;
   100 
   101 
   102 /* Streamer */
   103 typedef struct
   104 {
   105     Uint8 *buffer;
   106     int max_len;                /* the maximum length in bytes */
   107     int read_pos, write_pos;    /* the position of the write and read heads in bytes */
   108 } SDL_AudioStreamer;
   109 
   110 
   111 /* Define the SDL audio driver structure */
   112 struct SDL_AudioDevice
   113 {
   114     /* * * */
   115     /* Data common to all devices */
   116 
   117     /* The current audio specification (shared with audio thread) */
   118     SDL_AudioSpec spec;
   119 
   120     /* An audio conversion block for audio format emulation */
   121     SDL_AudioCVT convert;
   122 
   123     /* The streamer, if sample rate conversion necessitates it */
   124     int use_streamer;
   125     SDL_AudioStreamer streamer;
   126 
   127     /* Current state flags */
   128     int iscapture;
   129     int enabled;
   130     int paused;
   131     int opened;
   132 
   133     /* Fake audio buffer for when the audio hardware is busy */
   134     Uint8 *fake_stream;
   135 
   136     /* A semaphore for locking the mixing buffers */
   137     SDL_mutex *mixer_lock;
   138 
   139     /* A thread to feed the audio device */
   140     SDL_Thread *thread;
   141     SDL_threadID threadid;
   142 
   143     /* Queued buffers (if app not using callback). */
   144     SDL_AudioBufferQueue *buffer_queue_head; /* device fed from here. */
   145     SDL_AudioBufferQueue *buffer_queue_tail; /* queue fills to here. */
   146     SDL_AudioBufferQueue *buffer_queue_pool; /* these are unused packets. */
   147     Uint32 queued_bytes;  /* number of bytes of audio data in the queue. */
   148 
   149     /* * * */
   150     /* Data private to this driver */
   151     struct SDL_PrivateAudioData *hidden;
   152 };
   153 #undef _THIS
   154 
   155 typedef struct AudioBootStrap
   156 {
   157     const char *name;
   158     const char *desc;
   159     int (*init) (SDL_AudioDriverImpl * impl);
   160     int demand_only;  /* 1==request explicitly, or it won't be available. */
   161 } AudioBootStrap;
   162 
   163 #endif /* _SDL_sysaudio_h */
   164 
   165 /* vi: set ts=4 sw=4 expandtab: */