SDL_sysaudio.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../SDL_internal.h"
  19. #ifndef _SDL_sysaudio_h
  20. #define _SDL_sysaudio_h
  21. #include "SDL_mutex.h"
  22. #include "SDL_thread.h"
  23. /* !!! FIXME: These are wordy and unlocalized... */
  24. #define DEFAULT_OUTPUT_DEVNAME "System audio output device"
  25. #define DEFAULT_INPUT_DEVNAME "System audio capture device"
  26. /* The SDL audio driver */
  27. typedef struct SDL_AudioDevice SDL_AudioDevice;
  28. #define _THIS SDL_AudioDevice *_this
  29. /* Audio targets should call this as devices are added to the system (such as
  30. a USB headset being plugged in), and should also be called for
  31. for every device found during DetectDevices(). */
  32. extern void SDL_AddAudioDevice(const int iscapture, const char *name, void *handle);
  33. /* Audio targets should call this as devices are removed, so SDL can update
  34. its list of available devices. */
  35. extern void SDL_RemoveAudioDevice(const int iscapture, void *handle);
  36. /* Audio targets should call this if an opened audio device is lost while
  37. being used. This can happen due to i/o errors, or a device being unplugged,
  38. etc. If the device is totally gone, please also call SDL_RemoveAudioDevice()
  39. as appropriate so SDL's list of devices is accurate. */
  40. extern void SDL_OpenedAudioDeviceDisconnected(SDL_AudioDevice *device);
  41. /* This is the size of a packet when using SDL_QueueAudio(). We allocate
  42. these as necessary and pool them, under the assumption that we'll
  43. eventually end up with a handful that keep recycling, meeting whatever
  44. the app needs. We keep packing data tightly as more arrives to avoid
  45. wasting space, and if we get a giant block of data, we'll split them
  46. into multiple packets behind the scenes. My expectation is that most
  47. apps will have 2-3 of these in the pool. 8k should cover most needs, but
  48. if this is crippling for some embedded system, we can #ifdef this.
  49. The system preallocates enough packets for 2 callbacks' worth of data. */
  50. #define SDL_AUDIOBUFFERQUEUE_PACKETLEN (8 * 1024)
  51. /* Used by apps that queue audio instead of using the callback. */
  52. typedef struct SDL_AudioBufferQueue
  53. {
  54. Uint8 data[SDL_AUDIOBUFFERQUEUE_PACKETLEN]; /* packet data. */
  55. Uint32 datalen; /* bytes currently in use in this packet. */
  56. Uint32 startpos; /* bytes currently consumed in this packet. */
  57. struct SDL_AudioBufferQueue *next; /* next item in linked list. */
  58. } SDL_AudioBufferQueue;
  59. typedef struct SDL_AudioDriverImpl
  60. {
  61. void (*DetectDevices) (void);
  62. int (*OpenDevice) (_THIS, void *handle, const char *devname, int iscapture);
  63. void (*ThreadInit) (_THIS); /* Called by audio thread at start */
  64. void (*WaitDevice) (_THIS);
  65. void (*PlayDevice) (_THIS);
  66. int (*GetPendingBytes) (_THIS);
  67. Uint8 *(*GetDeviceBuf) (_THIS);
  68. void (*WaitDone) (_THIS);
  69. int (*CaptureFromDevice) (_THIS, void *buffer, int buflen);
  70. void (*FlushCapture) (_THIS);
  71. void (*CloseDevice) (_THIS);
  72. void (*LockDevice) (_THIS);
  73. void (*UnlockDevice) (_THIS);
  74. void (*FreeDeviceHandle) (void *handle); /**< SDL is done with handle from SDL_AddAudioDevice() */
  75. void (*Deinitialize) (void);
  76. /* !!! FIXME: add pause(), so we can optimize instead of mixing silence. */
  77. /* Some flags to push duplicate code into the core and reduce #ifdefs. */
  78. /* !!! FIXME: these should be SDL_bool */
  79. int ProvidesOwnCallbackThread;
  80. int SkipMixerLock; /* !!! FIXME: do we need this anymore? */
  81. int HasCaptureSupport;
  82. int OnlyHasDefaultOutputDevice;
  83. int OnlyHasDefaultCaptureDevice;
  84. int AllowsArbitraryDeviceNames;
  85. } SDL_AudioDriverImpl;
  86. typedef struct SDL_AudioDeviceItem
  87. {
  88. void *handle;
  89. struct SDL_AudioDeviceItem *next;
  90. #if (defined(__GNUC__) && (__GNUC__ <= 2))
  91. char name[1]; /* actually variable length. */
  92. #else
  93. char name[];
  94. #endif
  95. } SDL_AudioDeviceItem;
  96. typedef struct SDL_AudioDriver
  97. {
  98. /* * * */
  99. /* The name of this audio driver */
  100. const char *name;
  101. /* * * */
  102. /* The description of this audio driver */
  103. const char *desc;
  104. SDL_AudioDriverImpl impl;
  105. /* A mutex for device detection */
  106. SDL_mutex *detectionLock;
  107. SDL_bool captureDevicesRemoved;
  108. SDL_bool outputDevicesRemoved;
  109. int outputDeviceCount;
  110. int inputDeviceCount;
  111. SDL_AudioDeviceItem *outputDevices;
  112. SDL_AudioDeviceItem *inputDevices;
  113. } SDL_AudioDriver;
  114. /* Streamer */
  115. typedef struct
  116. {
  117. Uint8 *buffer;
  118. int max_len; /* the maximum length in bytes */
  119. int read_pos, write_pos; /* the position of the write and read heads in bytes */
  120. } SDL_AudioStreamer;
  121. /* Define the SDL audio driver structure */
  122. struct SDL_AudioDevice
  123. {
  124. /* * * */
  125. /* Data common to all devices */
  126. SDL_AudioDeviceID id;
  127. /* The current audio specification (shared with audio thread) */
  128. SDL_AudioSpec spec;
  129. /* An audio conversion block for audio format emulation */
  130. SDL_AudioCVT convert;
  131. /* The streamer, if sample rate conversion necessitates it */
  132. int use_streamer;
  133. SDL_AudioStreamer streamer;
  134. /* Current state flags */
  135. SDL_atomic_t shutdown; /* true if we are signaling the play thread to end. */
  136. SDL_atomic_t enabled; /* true if device is functioning and connected. */
  137. SDL_atomic_t paused;
  138. SDL_bool iscapture;
  139. /* Fake audio buffer for when the audio hardware is busy */
  140. Uint8 *fake_stream;
  141. /* A mutex for locking the mixing buffers */
  142. SDL_mutex *mixer_lock;
  143. /* A thread to feed the audio device */
  144. SDL_Thread *thread;
  145. SDL_threadID threadid;
  146. /* Queued buffers (if app not using callback). */
  147. SDL_AudioBufferQueue *buffer_queue_head; /* device fed from here. */
  148. SDL_AudioBufferQueue *buffer_queue_tail; /* queue fills to here. */
  149. SDL_AudioBufferQueue *buffer_queue_pool; /* these are unused packets. */
  150. Uint32 queued_bytes; /* number of bytes of audio data in the queue. */
  151. /* * * */
  152. /* Data private to this driver */
  153. struct SDL_PrivateAudioData *hidden;
  154. };
  155. #undef _THIS
  156. typedef struct AudioBootStrap
  157. {
  158. const char *name;
  159. const char *desc;
  160. int (*init) (SDL_AudioDriverImpl * impl);
  161. int demand_only; /* 1==request explicitly, or it won't be available. */
  162. } AudioBootStrap;
  163. #endif /* _SDL_sysaudio_h */
  164. /* vi: set ts=4 sw=4 expandtab: */