SDL_sysaudio.h
Go to the documentation of this file.
/*
Simple DirectMedia Layer
Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../SDL_internal.h"
#ifndef _SDL_sysaudio_h
#define _SDL_sysaudio_h
#include "SDL_mutex.h"
#include "SDL_thread.h"
/* The SDL audio driver */
typedef struct SDL_AudioDevice SDL_AudioDevice;
#define _THIS SDL_AudioDevice *_this
/* Audio targets should call this as devices are added to the system (such as
a USB headset being plugged in), and should also be called for
for every device found during DetectDevices(). */
extern void SDL_AddAudioDevice(const int iscapture, const char *name, void *handle);
/* Audio targets should call this as devices are removed, so SDL can update
its list of available devices. */
extern void SDL_RemoveAudioDevice(const int iscapture, void *handle);
/* Audio targets should call this if an opened audio device is lost while
being used. This can happen due to i/o errors, or a device being unplugged,
etc. If the device is totally gone, please also call SDL_RemoveAudioDevice()
as appropriate so SDL's list of devices is accurate. */
extern void SDL_OpenedAudioDeviceDisconnected(SDL_AudioDevice *device);
/* This is the size of a packet when using SDL_QueueAudio(). We allocate
these as necessary and pool them, under the assumption that we'll
eventually end up with a handful that keep recycling, meeting whatever
the app needs. We keep packing data tightly as more arrives to avoid
wasting space, and if we get a giant block of data, we'll split them
into multiple packets behind the scenes. My expectation is that most
apps will have 2-3 of these in the pool. 8k should cover most needs, but
if this is crippling for some embedded system, we can #ifdef this.
The system preallocates enough packets for 2 callbacks' worth of data. */
#define SDL_AUDIOBUFFERQUEUE_PACKETLEN (8 * 1024)
/* Used by apps that queue audio instead of using the callback. */
typedef struct SDL_AudioBufferQueue
{
Uint8 data[SDL_AUDIOBUFFERQUEUE_PACKETLEN]; /* packet data. */
Uint32 datalen; /* bytes currently in use in this packet. */
Uint32 startpos; /* bytes currently consumed in this packet. */
struct SDL_AudioBufferQueue *next; /* next item in linked list. */
} SDL_AudioBufferQueue;
typedef struct SDL_AudioDriverImpl
{
void (*DetectDevices) (void);
int (*OpenDevice) (_THIS, void *handle, const char *devname, int iscapture);
void (*ThreadInit) (_THIS); /* Called by audio thread at start */
void (*WaitDevice) (_THIS);
void (*PlayDevice) (_THIS);
int (*GetPendingBytes) (_THIS);
Uint8 *(*GetDeviceBuf) (_THIS);
void (*WaitDone) (_THIS);
void (*CloseDevice) (_THIS);
void (*LockDevice) (_THIS);
void (*UnlockDevice) (_THIS);
void (*FreeDeviceHandle) (void *handle); /**< SDL is done with handle from SDL_AddAudioDevice() */
void (*Deinitialize) (void);
/* !!! FIXME: add pause(), so we can optimize instead of mixing silence. */
/* Some flags to push duplicate code into the core and reduce #ifdefs. */
/* !!! FIXME: these should be SDL_bool */
int ProvidesOwnCallbackThread;
int SkipMixerLock; /* !!! FIXME: do we need this anymore? */
int HasCaptureSupport;
int OnlyHasDefaultOutputDevice;
int OnlyHasDefaultInputDevice;
int AllowsArbitraryDeviceNames;
} SDL_AudioDriverImpl;
typedef struct SDL_AudioDeviceItem
{
void *handle;
struct SDL_AudioDeviceItem *next;
#if (defined(__GNUC__) && (__GNUC__ <= 2))
char name[1]; /* actually variable length. */
#else
char name[];
#endif
} SDL_AudioDeviceItem;
typedef struct SDL_AudioDriver
{
/* * * */
/* The name of this audio driver */
const char *name;
/* * * */
/* The description of this audio driver */
const char *desc;
SDL_AudioDriverImpl impl;
/* A mutex for device detection */
SDL_mutex *detectionLock;
SDL_bool captureDevicesRemoved;
SDL_bool outputDevicesRemoved;
int outputDeviceCount;
int inputDeviceCount;
SDL_AudioDeviceItem *outputDevices;
SDL_AudioDeviceItem *inputDevices;
} SDL_AudioDriver;
/* Streamer */
typedef struct
{
Uint8 *buffer;
int max_len; /* the maximum length in bytes */
int read_pos, write_pos; /* the position of the write and read heads in bytes */
} SDL_AudioStreamer;
/* Define the SDL audio driver structure */
struct SDL_AudioDevice
{
/* * * */
/* Data common to all devices */
SDL_AudioDeviceID id;
/* The current audio specification (shared with audio thread) */
SDL_AudioSpec spec;
/* An audio conversion block for audio format emulation */
SDL_AudioCVT convert;
/* The streamer, if sample rate conversion necessitates it */
int use_streamer;
SDL_AudioStreamer streamer;
/* Current state flags */
/* !!! FIXME: should be SDL_bool */
int iscapture;
int enabled; /* true if device is functioning and connected. */
int shutdown; /* true if we are signaling the play thread to end. */
int paused;
int opened;
/* Fake audio buffer for when the audio hardware is busy */
Uint8 *fake_stream;
/* A mutex for locking the mixing buffers */
SDL_mutex *mixer_lock;
/* A thread to feed the audio device */
SDL_Thread *thread;
SDL_threadID threadid;
/* Queued buffers (if app not using callback). */
SDL_AudioBufferQueue *buffer_queue_head; /* device fed from here. */
SDL_AudioBufferQueue *buffer_queue_tail; /* queue fills to here. */
SDL_AudioBufferQueue *buffer_queue_pool; /* these are unused packets. */
Uint32 queued_bytes; /* number of bytes of audio data in the queue. */
/* * * */
/* Data private to this driver */
struct SDL_PrivateAudioData *hidden;
};
#undef _THIS
typedef struct AudioBootStrap
{
const char *name;
const char *desc;
int (*init) (SDL_AudioDriverImpl * impl);
int demand_only; /* 1==request explicitly, or it won't be available. */
} AudioBootStrap;
#endif /* _SDL_sysaudio_h */
/* vi: set ts=4 sw=4 expandtab: */
SDL_AudioDevice::hidden
struct SDL_PrivateAudioData * hidden
Definition: SDL_sysaudio.h:185
SDL_AudioDevice::id
SDL_AudioDeviceID id
Definition: SDL_sysaudio.h:147
SDL_AudioBufferQueue::datalen
Uint32 datalen
Definition: SDL_sysaudio.h:64
SDL_AudioDevice::mixer_lock
SDL_mutex * mixer_lock
Definition: SDL_sysaudio.h:171
SDL_AudioDriver::captureDevicesRemoved
SDL_bool captureDevicesRemoved
Definition: SDL_sysaudio.h:124
SDL_AudioStreamer::buffer
Uint8 * buffer
Definition: SDL_sysaudio.h:136
SDL_AudioDriver::name
const char * name
Definition: SDL_sysaudio.h:114
SDL_AudioStreamer::max_len
int max_len
Definition: SDL_sysaudio.h:137
Uint32
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:157
AudioBootStrap::name
const char * name
Definition: SDL_sysaudio.h:191
SDL_AudioDeviceItem
Definition: SDL_sysaudio.h:98
SDL_AudioDeviceItem::next
struct SDL_AudioDeviceItem * next
Definition: SDL_sysaudio.h:101
SDL_AudioDriverImpl::HasCaptureSupport
int HasCaptureSupport
Definition: SDL_sysaudio.h:91
SDL_AudioDriver::inputDeviceCount
int inputDeviceCount
Definition: SDL_sysaudio.h:127
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1967
SDL_AudioDevice::opened
int opened
Definition: SDL_sysaudio.h:165
SDL_AudioDriverImpl::ProvidesOwnCallbackThread
int ProvidesOwnCallbackThread
Definition: SDL_sysaudio.h:89
SDL_AudioDevice::shutdown
int shutdown
Definition: SDL_sysaudio.h:163
SDL_AudioSpec
Definition: SDL_audio.h:168
SDL_AudioDevice::threadid
SDL_threadID threadid
Definition: SDL_sysaudio.h:175
SDL_AudioDevice::buffer_queue_pool
SDL_AudioBufferQueue * buffer_queue_pool
Definition: SDL_sysaudio.h:180
SDL_AudioStreamer::write_pos
int write_pos
Definition: SDL_sysaudio.h:138
SDL_mutex.h
SDL_AudioDevice::spec
SDL_AudioSpec spec
Definition: SDL_sysaudio.h:150
SDL_AddAudioDevice
void SDL_AddAudioDevice(const int iscapture, const char *name, void *handle)
Definition: SDL_audio.c:347
name
GLuint const GLchar * name
Definition: SDL_opengl_glext.h:660
SDL_AudioDevice::iscapture
int iscapture
Definition: SDL_sysaudio.h:161
SDL_AudioDeviceItem::handle
void * handle
Definition: SDL_sysaudio.h:100
SDL_PrivateAudioData
Definition: SDL_alsa_audio.h:33
SDL_thread.h
SDL_AudioDevice::use_streamer
int use_streamer
Definition: SDL_sysaudio.h:156
SDL_AudioCVT
Definition: SDL_audio.h:200
SDL_mutex
Definition: SDL_sysmutex.c:29
SDL_AudioDriver::detectionLock
SDL_mutex * detectionLock
Definition: SDL_sysaudio.h:123
SDL_AudioDevice::queued_bytes
Uint32 queued_bytes
Definition: SDL_sysaudio.h:181
SDL_AudioDevice::buffer_queue_head
SDL_AudioBufferQueue * buffer_queue_head
Definition: SDL_sysaudio.h:178
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:126
SDL_RemoveAudioDevice
void SDL_RemoveAudioDevice(const int iscapture, void *handle)
Definition: SDL_audio.c:405
SDL_AudioDevice::buffer_queue_tail
SDL_AudioBufferQueue * buffer_queue_tail
Definition: SDL_sysaudio.h:179
SDL_AudioBufferQueue
Definition: SDL_sysaudio.h:61
AudioBootStrap
Definition: SDL_sysaudio.h:189
SDL_AudioBufferQueue::next
struct SDL_AudioBufferQueue * next
Definition: SDL_sysaudio.h:66
SDL_AudioDriverImpl::OnlyHasDefaultOutputDevice
int OnlyHasDefaultOutputDevice
Definition: SDL_sysaudio.h:92
Uint8
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:139
SDL_AudioDriver::desc
const char * desc
Definition: SDL_sysaudio.h:118
SDL_AudioDriverImpl
Definition: SDL_sysaudio.h:69
_THIS
#define _THIS
Definition: SDL_sysaudio.h:31
SDL_AudioDriverImpl::SkipMixerLock
int SkipMixerLock
Definition: SDL_sysaudio.h:90
SDL_AudioStreamer
Definition: SDL_sysaudio.h:134
SDL_AudioDevice::fake_stream
Uint8 * fake_stream
Definition: SDL_sysaudio.h:168
SDL_AudioDriver::outputDevices
SDL_AudioDeviceItem * outputDevices
Definition: SDL_sysaudio.h:128
SDL_AudioDevice::streamer
SDL_AudioStreamer streamer
Definition: SDL_sysaudio.h:157
SDL_AudioDriver::outputDevicesRemoved
SDL_bool outputDevicesRemoved
Definition: SDL_sysaudio.h:125
void
SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 void
Definition: SDL_dynapi_procs.h:93
SDL_AudioDriverImpl::AllowsArbitraryDeviceNames
int AllowsArbitraryDeviceNames
Definition: SDL_sysaudio.h:94
SDL_AudioDeviceID
Uint32 SDL_AudioDeviceID
Definition: SDL_audio.h:303
SDL_AudioDriver::impl
SDL_AudioDriverImpl impl
Definition: SDL_sysaudio.h:120
SDL_AudioDevice
Definition: SDL_sysaudio.h:143
SDL_AudioDevice::paused
int paused
Definition: SDL_sysaudio.h:164
SDL_AudioBufferQueue::startpos
Uint32 startpos
Definition: SDL_sysaudio.h:65
SDL_AudioDriver::outputDeviceCount
int outputDeviceCount
Definition: SDL_sysaudio.h:126
SDL_OpenedAudioDeviceDisconnected
void SDL_OpenedAudioDeviceDisconnected(SDL_AudioDevice *device)
Definition: SDL_audio.c:364
AudioBootStrap::demand_only
int demand_only
Definition: SDL_sysaudio.h:194
SDL_Thread
Definition: SDL_thread_c.h:54
AudioBootStrap::desc
const char * desc
Definition: SDL_sysaudio.h:192
SDL_AudioDriverImpl::OnlyHasDefaultInputDevice
int OnlyHasDefaultInputDevice
Definition: SDL_sysaudio.h:93
SDL_AudioDevice::thread
SDL_Thread * thread
Definition: SDL_sysaudio.h:174
SDL_AudioDevice::enabled
int enabled
Definition: SDL_sysaudio.h:162
SDL_AUDIOBUFFERQUEUE_PACKETLEN
#define SDL_AUDIOBUFFERQUEUE_PACKETLEN
Definition: SDL_sysaudio.h:58
SDL_AudioDevice::convert
SDL_AudioCVT convert
Definition: SDL_sysaudio.h:153
SDL_AudioDriver
Definition: SDL_sysaudio.h:110
SDL_AudioDriver::inputDevices
SDL_AudioDeviceItem * inputDevices
Definition: SDL_sysaudio.h:129
SDL_threadID
unsigned long SDL_threadID
Definition: SDL_thread.h:49