1
0

SDL_sysaudio.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 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. #include "../SDL_dataqueue.h"
  24. #include "./SDL_audio_c.h"
  25. /* !!! FIXME: These are wordy and unlocalized... */
  26. #define DEFAULT_OUTPUT_DEVNAME "System audio output device"
  27. #define DEFAULT_INPUT_DEVNAME "System audio capture device"
  28. /* The SDL audio driver */
  29. typedef struct SDL_AudioDevice SDL_AudioDevice;
  30. #define _THIS SDL_AudioDevice *_this
  31. /* Audio targets should call this as devices are added to the system (such as
  32. a USB headset being plugged in), and should also be called for
  33. for every device found during DetectDevices(). */
  34. extern void SDL_AddAudioDevice(const SDL_bool iscapture, const char *name, SDL_AudioSpec *spec, void *handle);
  35. /* Audio targets should call this as devices are removed, so SDL can update
  36. its list of available devices. */
  37. extern void SDL_RemoveAudioDevice(const SDL_bool iscapture, void *handle);
  38. /* Audio targets should call this if an opened audio device is lost while
  39. being used. This can happen due to i/o errors, or a device being unplugged,
  40. etc. If the device is totally gone, please also call SDL_RemoveAudioDevice()
  41. as appropriate so SDL's list of devices is accurate. */
  42. extern void SDL_OpenedAudioDeviceDisconnected(SDL_AudioDevice *device);
  43. /* This is the size of a packet when using SDL_QueueAudio(). We allocate
  44. these as necessary and pool them, under the assumption that we'll
  45. eventually end up with a handful that keep recycling, meeting whatever
  46. the app needs. We keep packing data tightly as more arrives to avoid
  47. wasting space, and if we get a giant block of data, we'll split them
  48. into multiple packets behind the scenes. My expectation is that most
  49. apps will have 2-3 of these in the pool. 8k should cover most needs, but
  50. if this is crippling for some embedded system, we can #ifdef this.
  51. The system preallocates enough packets for 2 callbacks' worth of data. */
  52. #define SDL_AUDIOBUFFERQUEUE_PACKETLEN (8 * 1024)
  53. typedef struct SDL_AudioDriverImpl
  54. {
  55. void (*DetectDevices)(void);
  56. int (*OpenDevice)(_THIS, const char *devname);
  57. void (*ThreadInit)(_THIS); /* Called by audio thread at start */
  58. void (*ThreadDeinit)(_THIS); /* Called by audio thread at end */
  59. void (*WaitDevice)(_THIS);
  60. void (*PlayDevice)(_THIS);
  61. Uint8 *(*GetDeviceBuf)(_THIS);
  62. int (*CaptureFromDevice)(_THIS, void *buffer, int buflen);
  63. void (*FlushCapture)(_THIS);
  64. void (*CloseDevice)(_THIS);
  65. void (*LockDevice)(_THIS);
  66. void (*UnlockDevice)(_THIS);
  67. void (*FreeDeviceHandle)(void *handle); /**< SDL is done with handle from SDL_AddAudioDevice() */
  68. void (*Deinitialize)(void);
  69. int (*GetDefaultAudioInfo)(char **name, SDL_AudioSpec *spec, int iscapture);
  70. /* !!! FIXME: add pause(), so we can optimize instead of mixing silence. */
  71. /* Some flags to push duplicate code into the core and reduce #ifdefs. */
  72. SDL_bool ProvidesOwnCallbackThread;
  73. SDL_bool HasCaptureSupport;
  74. SDL_bool OnlyHasDefaultOutputDevice;
  75. SDL_bool OnlyHasDefaultCaptureDevice;
  76. SDL_bool AllowsArbitraryDeviceNames;
  77. SDL_bool SupportsNonPow2Samples;
  78. } SDL_AudioDriverImpl;
  79. typedef struct SDL_AudioDeviceItem
  80. {
  81. void *handle;
  82. char *name;
  83. char *original_name;
  84. SDL_AudioSpec spec;
  85. int dupenum;
  86. struct SDL_AudioDeviceItem *next;
  87. } SDL_AudioDeviceItem;
  88. typedef struct SDL_AudioDriver
  89. {
  90. /* * * */
  91. /* The name of this audio driver */
  92. const char *name;
  93. /* * * */
  94. /* The description of this audio driver */
  95. const char *desc;
  96. SDL_AudioDriverImpl impl;
  97. /* A mutex for device detection */
  98. SDL_mutex *detectionLock;
  99. SDL_bool captureDevicesRemoved;
  100. SDL_bool outputDevicesRemoved;
  101. int outputDeviceCount;
  102. int inputDeviceCount;
  103. SDL_AudioDeviceItem *outputDevices;
  104. SDL_AudioDeviceItem *inputDevices;
  105. } SDL_AudioDriver;
  106. /* Define the SDL audio driver structure */
  107. struct SDL_AudioDevice
  108. {
  109. /* * * */
  110. /* Data common to all devices */
  111. SDL_AudioDeviceID id;
  112. /* The device's current audio specification */
  113. SDL_AudioSpec spec;
  114. /* The callback's expected audio specification (converted vs device's spec). */
  115. SDL_AudioSpec callbackspec;
  116. /* Stream that converts and resamples. NULL if not needed. */
  117. SDL_AudioStream *stream;
  118. /* Current state flags */
  119. SDL_atomic_t shutdown; /* true if we are signaling the play thread to end. */
  120. SDL_atomic_t enabled; /* true if device is functioning and connected. */
  121. SDL_atomic_t paused;
  122. SDL_bool iscapture;
  123. /* Scratch buffer used in the bridge between SDL and the user callback. */
  124. Uint8 *work_buffer;
  125. /* Size, in bytes, of work_buffer. */
  126. Uint32 work_buffer_len;
  127. /* A mutex for locking the mixing buffers */
  128. SDL_mutex *mixer_lock;
  129. /* A thread to feed the audio device */
  130. SDL_Thread *thread;
  131. SDL_threadID threadid;
  132. /* Queued buffers (if app not using callback). */
  133. SDL_DataQueue *buffer_queue;
  134. /* * * */
  135. /* Data private to this driver */
  136. struct SDL_PrivateAudioData *hidden;
  137. void *handle;
  138. };
  139. #undef _THIS
  140. typedef struct AudioBootStrap
  141. {
  142. const char *name;
  143. const char *desc;
  144. SDL_bool (*init)(SDL_AudioDriverImpl *impl);
  145. SDL_bool demand_only; /* 1==request explicitly, or it won't be available. */
  146. } AudioBootStrap;
  147. /* Not all of these are available in a given build. Use #ifdefs, etc. */
  148. extern AudioBootStrap PIPEWIRE_bootstrap;
  149. extern AudioBootStrap PULSEAUDIO_bootstrap;
  150. extern AudioBootStrap ALSA_bootstrap;
  151. extern AudioBootStrap JACK_bootstrap;
  152. extern AudioBootStrap SNDIO_bootstrap;
  153. extern AudioBootStrap NETBSDAUDIO_bootstrap;
  154. extern AudioBootStrap DSP_bootstrap;
  155. extern AudioBootStrap QSAAUDIO_bootstrap;
  156. extern AudioBootStrap SUNAUDIO_bootstrap;
  157. extern AudioBootStrap ARTS_bootstrap;
  158. extern AudioBootStrap ESD_bootstrap;
  159. extern AudioBootStrap NACLAUDIO_bootstrap;
  160. extern AudioBootStrap NAS_bootstrap;
  161. extern AudioBootStrap WASAPI_bootstrap;
  162. extern AudioBootStrap DSOUND_bootstrap;
  163. extern AudioBootStrap WINMM_bootstrap;
  164. extern AudioBootStrap PAUDIO_bootstrap;
  165. extern AudioBootStrap HAIKUAUDIO_bootstrap;
  166. extern AudioBootStrap COREAUDIO_bootstrap;
  167. extern AudioBootStrap DISKAUDIO_bootstrap;
  168. extern AudioBootStrap DUMMYAUDIO_bootstrap;
  169. extern AudioBootStrap FUSIONSOUND_bootstrap;
  170. extern AudioBootStrap aaudio_bootstrap;
  171. extern AudioBootStrap openslES_bootstrap;
  172. extern AudioBootStrap ANDROIDAUDIO_bootstrap;
  173. extern AudioBootStrap PS2AUDIO_bootstrap;
  174. extern AudioBootStrap PSPAUDIO_bootstrap;
  175. extern AudioBootStrap VITAAUD_bootstrap;
  176. extern AudioBootStrap N3DSAUDIO_bootstrap;
  177. extern AudioBootStrap EMSCRIPTENAUDIO_bootstrap;
  178. extern AudioBootStrap OS2AUDIO_bootstrap;
  179. #endif /* SDL_sysaudio_h_ */
  180. /* vi: set ts=4 sw=4 expandtab: */