src/audio/SDL_sysaudio.h
author Ryan C. Gordon <icculus@icculus.org>
Mon, 21 May 2018 12:00:21 -0400
changeset 11994 8e094f91ddab
parent 11811 5d94cb6b24d3
child 12315 5ce13990c5fb
permissions -rw-r--r--
thread: fixed compiler warnings on non-Linux systems that use pthread.

(static function rtkit_setpriority was unused, moved it in with rest of
__LINUX__ section.)
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@11811
     3
  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@0
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
icculus@8093
    21
#include "../SDL_internal.h"
slouken@0
    22
slouken@10638
    23
#ifndef SDL_sysaudio_h_
slouken@10638
    24
#define SDL_sysaudio_h_
slouken@0
    25
slouken@0
    26
#include "SDL_mutex.h"
slouken@0
    27
#include "SDL_thread.h"
icculus@10681
    28
#include "../SDL_dataqueue.h"
icculus@10758
    29
#include "./SDL_audio_c.h"
slouken@0
    30
icculus@10264
    31
/* !!! FIXME: These are wordy and unlocalized... */
icculus@10264
    32
#define DEFAULT_OUTPUT_DEVNAME "System audio output device"
icculus@10264
    33
#define DEFAULT_INPUT_DEVNAME "System audio capture device"
icculus@10264
    34
slouken@0
    35
/* The SDL audio driver */
slouken@0
    36
typedef struct SDL_AudioDevice SDL_AudioDevice;
slouken@7191
    37
#define _THIS   SDL_AudioDevice *_this
slouken@0
    38
icculus@9394
    39
/* Audio targets should call this as devices are added to the system (such as
icculus@9394
    40
   a USB headset being plugged in), and should also be called for
icculus@9394
    41
   for every device found during DetectDevices(). */
icculus@9394
    42
extern void SDL_AddAudioDevice(const int iscapture, const char *name, void *handle);
icculus@9393
    43
icculus@9394
    44
/* Audio targets should call this as devices are removed, so SDL can update
icculus@9394
    45
   its list of available devices. */
icculus@9399
    46
extern void SDL_RemoveAudioDevice(const int iscapture, void *handle);
icculus@9393
    47
icculus@9394
    48
/* Audio targets should call this if an opened audio device is lost while
icculus@9394
    49
   being used. This can happen due to i/o errors, or a device being unplugged,
icculus@9394
    50
   etc. If the device is totally gone, please also call SDL_RemoveAudioDevice()
icculus@9394
    51
   as appropriate so SDL's list of devices is accurate. */
icculus@9394
    52
extern void SDL_OpenedAudioDeviceDisconnected(SDL_AudioDevice *device);
icculus@9393
    53
icculus@9012
    54
/* This is the size of a packet when using SDL_QueueAudio(). We allocate
icculus@9012
    55
   these as necessary and pool them, under the assumption that we'll
icculus@9012
    56
   eventually end up with a handful that keep recycling, meeting whatever
icculus@9012
    57
   the app needs. We keep packing data tightly as more arrives to avoid
icculus@9012
    58
   wasting space, and if we get a giant block of data, we'll split them
icculus@9012
    59
   into multiple packets behind the scenes. My expectation is that most
icculus@9012
    60
   apps will have 2-3 of these in the pool. 8k should cover most needs, but
icculus@9012
    61
   if this is crippling for some embedded system, we can #ifdef this.
icculus@9012
    62
   The system preallocates enough packets for 2 callbacks' worth of data. */
icculus@9012
    63
#define SDL_AUDIOBUFFERQUEUE_PACKETLEN (8 * 1024)
icculus@9012
    64
icculus@2049
    65
typedef struct SDL_AudioDriverImpl
icculus@2049
    66
{
icculus@9394
    67
    void (*DetectDevices) (void);
icculus@9394
    68
    int (*OpenDevice) (_THIS, void *handle, const char *devname, int iscapture);
icculus@2049
    69
    void (*ThreadInit) (_THIS); /* Called by audio thread at start */
philipp@10900
    70
    void (*ThreadDeinit) (_THIS); /* Called by audio thread at end */
icculus@11726
    71
    void (*BeginLoopIteration)(_THIS);  /* Called by audio thread at top of loop */
icculus@2049
    72
    void (*WaitDevice) (_THIS);
icculus@2049
    73
    void (*PlayDevice) (_THIS);
icculus@9031
    74
    int (*GetPendingBytes) (_THIS);
icculus@2049
    75
    Uint8 *(*GetDeviceBuf) (_THIS);
icculus@10239
    76
    int (*CaptureFromDevice) (_THIS, void *buffer, int buflen);
icculus@10239
    77
    void (*FlushCapture) (_THIS);
icculus@10473
    78
    void (*PrepareToClose) (_THIS);  /**< Called between run and draining wait for playback devices */
icculus@2049
    79
    void (*CloseDevice) (_THIS);
icculus@2049
    80
    void (*LockDevice) (_THIS);
icculus@2049
    81
    void (*UnlockDevice) (_THIS);
icculus@9394
    82
    void (*FreeDeviceHandle) (void *handle);  /**< SDL is done with handle from SDL_AddAudioDevice() */
icculus@2049
    83
    void (*Deinitialize) (void);
icculus@2049
    84
icculus@5590
    85
    /* !!! FIXME: add pause(), so we can optimize instead of mixing silence. */
icculus@5590
    86
icculus@2049
    87
    /* Some flags to push duplicate code into the core and reduce #ifdefs. */
icculus@9391
    88
    /* !!! FIXME: these should be SDL_bool */
icculus@5589
    89
    int ProvidesOwnCallbackThread;
icculus@10471
    90
    int SkipMixerLock;
icculus@5589
    91
    int HasCaptureSupport;
icculus@5589
    92
    int OnlyHasDefaultOutputDevice;
icculus@10258
    93
    int OnlyHasDefaultCaptureDevice;
icculus@9394
    94
    int AllowsArbitraryDeviceNames;
icculus@2049
    95
} SDL_AudioDriverImpl;
icculus@2049
    96
icculus@2049
    97
icculus@9394
    98
typedef struct SDL_AudioDeviceItem
icculus@9394
    99
{
icculus@9394
   100
    void *handle;
icculus@9394
   101
    struct SDL_AudioDeviceItem *next;
icculus@10680
   102
    char name[SDL_VARIABLE_LENGTH_ARRAY];
icculus@9394
   103
} SDL_AudioDeviceItem;
icculus@9394
   104
icculus@9394
   105
icculus@2049
   106
typedef struct SDL_AudioDriver
slouken@1895
   107
{
slouken@1895
   108
    /* * * */
slouken@1895
   109
    /* The name of this audio driver */
slouken@1895
   110
    const char *name;
slouken@0
   111
slouken@1895
   112
    /* * * */
slouken@1895
   113
    /* The description of this audio driver */
slouken@1895
   114
    const char *desc;
slouken@0
   115
icculus@2049
   116
    SDL_AudioDriverImpl impl;
icculus@5593
   117
icculus@9393
   118
    /* A mutex for device detection */
icculus@9394
   119
    SDL_mutex *detectionLock;
icculus@9394
   120
    SDL_bool captureDevicesRemoved;
icculus@9394
   121
    SDL_bool outputDevicesRemoved;
icculus@5593
   122
    int outputDeviceCount;
icculus@5593
   123
    int inputDeviceCount;
icculus@9394
   124
    SDL_AudioDeviceItem *outputDevices;
icculus@9394
   125
    SDL_AudioDeviceItem *inputDevices;
icculus@2049
   126
} SDL_AudioDriver;
slouken@0
   127
slouken@322
   128
icculus@2049
   129
/* Define the SDL audio driver structure */
icculus@2049
   130
struct SDL_AudioDevice
icculus@2049
   131
{
slouken@1895
   132
    /* * * */
slouken@1895
   133
    /* Data common to all devices */
icculus@9393
   134
    SDL_AudioDeviceID id;
slouken@0
   135
icculus@10757
   136
    /* The device's current audio specification */
slouken@1895
   137
    SDL_AudioSpec spec;
slouken@0
   138
icculus@10757
   139
    /* The callback's expected audio specification (converted vs device's spec). */
icculus@10757
   140
    SDL_AudioSpec callbackspec;
slouken@0
   141
icculus@10757
   142
    /* Stream that converts and resamples. NULL if not needed. */
icculus@10757
   143
    SDL_AudioStream *stream;
slouken@2716
   144
slouken@1895
   145
    /* Current state flags */
icculus@10233
   146
    SDL_atomic_t shutdown; /* true if we are signaling the play thread to end. */
icculus@10238
   147
    SDL_atomic_t enabled;  /* true if device is functioning and connected. */
icculus@10238
   148
    SDL_atomic_t paused;
icculus@10235
   149
    SDL_bool iscapture;
slouken@0
   150
icculus@10765
   151
    /* Scratch buffer used in the bridge between SDL and the user callback. */
icculus@10765
   152
    Uint8 *work_buffer;
slouken@0
   153
icculus@10765
   154
    /* Size, in bytes, of work_buffer. */
icculus@10765
   155
    Uint32 work_buffer_len;
icculus@10757
   156
icculus@9391
   157
    /* A mutex for locking the mixing buffers */
slouken@1895
   158
    SDL_mutex *mixer_lock;
slouken@0
   159
slouken@1895
   160
    /* A thread to feed the audio device */
slouken@1895
   161
    SDL_Thread *thread;
slouken@3578
   162
    SDL_threadID threadid;
slouken@0
   163
icculus@9012
   164
    /* Queued buffers (if app not using callback). */
icculus@10681
   165
    SDL_DataQueue *buffer_queue;
icculus@9012
   166
slouken@1895
   167
    /* * * */
slouken@1895
   168
    /* Data private to this driver */
slouken@1895
   169
    struct SDL_PrivateAudioData *hidden;
slouken@10467
   170
slouken@10467
   171
    void *handle;
slouken@0
   172
};
slouken@0
   173
#undef _THIS
slouken@0
   174
slouken@1895
   175
typedef struct AudioBootStrap
slouken@1895
   176
{
slouken@1895
   177
    const char *name;
slouken@1895
   178
    const char *desc;
slouken@2060
   179
    int (*init) (SDL_AudioDriverImpl * impl);
icculus@5578
   180
    int demand_only;  /* 1==request explicitly, or it won't be available. */
slouken@0
   181
} AudioBootStrap;
slouken@0
   182
icculus@10884
   183
/* Not all of these are available in a given build. Use #ifdefs, etc. */
slouken@10609
   184
extern AudioBootStrap PULSEAUDIO_bootstrap;
slouken@10609
   185
extern AudioBootStrap ALSA_bootstrap;
icculus@11081
   186
extern AudioBootStrap JACK_bootstrap;
slouken@10609
   187
extern AudioBootStrap SNDIO_bootstrap;
icculus@11028
   188
extern AudioBootStrap NETBSDAUDIO_bootstrap;
slouken@10609
   189
extern AudioBootStrap DSP_bootstrap;
slouken@10609
   190
extern AudioBootStrap QSAAUDIO_bootstrap;
slouken@10609
   191
extern AudioBootStrap SUNAUDIO_bootstrap;
slouken@10609
   192
extern AudioBootStrap ARTS_bootstrap;
slouken@10609
   193
extern AudioBootStrap ESD_bootstrap;
slouken@10609
   194
extern AudioBootStrap NACLAUDIO_bootstrap;
slouken@10609
   195
extern AudioBootStrap NAS_bootstrap;
icculus@10889
   196
extern AudioBootStrap WASAPI_bootstrap;
slouken@10609
   197
extern AudioBootStrap DSOUND_bootstrap;
slouken@10609
   198
extern AudioBootStrap WINMM_bootstrap;
slouken@10609
   199
extern AudioBootStrap PAUDIO_bootstrap;
slouken@10609
   200
extern AudioBootStrap HAIKUAUDIO_bootstrap;
slouken@10609
   201
extern AudioBootStrap COREAUDIO_bootstrap;
slouken@10609
   202
extern AudioBootStrap DISKAUDIO_bootstrap;
slouken@10609
   203
extern AudioBootStrap DUMMYAUDIO_bootstrap;
slouken@10609
   204
extern AudioBootStrap FUSIONSOUND_bootstrap;
slouken@10609
   205
extern AudioBootStrap ANDROIDAUDIO_bootstrap;
slouken@10609
   206
extern AudioBootStrap PSPAUDIO_bootstrap;
slouken@10609
   207
extern AudioBootStrap EMSCRIPTENAUDIO_bootstrap;
slouken@10609
   208
slouken@10638
   209
#endif /* SDL_sysaudio_h_ */
slouken@0
   210
slouken@1895
   211
/* vi: set ts=4 sw=4 expandtab: */