SDL_androidaudio.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2016 Sam Lantinga <[email protected]>
  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. #if SDL_AUDIO_DRIVER_ANDROID
  20. /* Output audio to Android */
  21. #include "SDL_assert.h"
  22. #include "SDL_audio.h"
  23. #include "../SDL_audio_c.h"
  24. #include "SDL_androidaudio.h"
  25. #include "../../core/android/SDL_android.h"
  26. #include <android/log.h>
  27. static SDL_AudioDevice* audioDevice = NULL;
  28. static SDL_AudioDevice* captureDevice = NULL;
  29. static int
  30. ANDROIDAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
  31. {
  32. SDL_AudioFormat test_format;
  33. SDL_assert((captureDevice == NULL) || !iscapture);
  34. SDL_assert((audioDevice == NULL) || iscapture);
  35. if (iscapture) {
  36. captureDevice = this;
  37. } else {
  38. audioDevice = this;
  39. }
  40. this->hidden = (struct SDL_PrivateAudioData *) SDL_calloc(1, (sizeof *this->hidden));
  41. if (this->hidden == NULL) {
  42. return SDL_OutOfMemory();
  43. }
  44. test_format = SDL_FirstAudioFormat(this->spec.format);
  45. while (test_format != 0) { /* no "UNKNOWN" constant */
  46. if ((test_format == AUDIO_U8) || (test_format == AUDIO_S16LSB)) {
  47. this->spec.format = test_format;
  48. break;
  49. }
  50. test_format = SDL_NextAudioFormat();
  51. }
  52. if (test_format == 0) {
  53. /* Didn't find a compatible format :( */
  54. return SDL_SetError("No compatible audio format!");
  55. }
  56. if (this->spec.channels > 1) {
  57. this->spec.channels = 2;
  58. } else {
  59. this->spec.channels = 1;
  60. }
  61. if (this->spec.freq < 8000) {
  62. this->spec.freq = 8000;
  63. }
  64. if (this->spec.freq > 48000) {
  65. this->spec.freq = 48000;
  66. }
  67. /* TODO: pass in/return a (Java) device ID */
  68. this->spec.samples = Android_JNI_OpenAudioDevice(iscapture, this->spec.freq, this->spec.format == AUDIO_U8 ? 0 : 1, this->spec.channels, this->spec.samples);
  69. if (this->spec.samples == 0) {
  70. /* Init failed? */
  71. return SDL_SetError("Java-side initialization failed!");
  72. }
  73. SDL_CalculateAudioSpec(&this->spec);
  74. return 0;
  75. }
  76. static void
  77. ANDROIDAUDIO_PlayDevice(_THIS)
  78. {
  79. Android_JNI_WriteAudioBuffer();
  80. }
  81. static Uint8 *
  82. ANDROIDAUDIO_GetDeviceBuf(_THIS)
  83. {
  84. return Android_JNI_GetAudioBuffer();
  85. }
  86. static int
  87. ANDROIDAUDIO_CaptureFromDevice(_THIS, void *buffer, int buflen)
  88. {
  89. return Android_JNI_CaptureAudioBuffer(buffer, buflen);
  90. }
  91. static void
  92. ANDROIDAUDIO_FlushCapture(_THIS)
  93. {
  94. Android_JNI_FlushCapturedAudio();
  95. }
  96. static void
  97. ANDROIDAUDIO_CloseDevice(_THIS)
  98. {
  99. /* At this point SDL_CloseAudioDevice via close_audio_device took care of terminating the audio thread
  100. so it's safe to terminate the Java side buffer and AudioTrack
  101. */
  102. Android_JNI_CloseAudioDevice(this->iscapture);
  103. if (this->iscapture) {
  104. SDL_assert(captureDevice == this);
  105. captureDevice = NULL;
  106. } else {
  107. SDL_assert(audioDevice == this);
  108. audioDevice = NULL;
  109. }
  110. SDL_free(this->hidden);
  111. }
  112. static int
  113. ANDROIDAUDIO_Init(SDL_AudioDriverImpl * impl)
  114. {
  115. /* Set the function pointers */
  116. impl->OpenDevice = ANDROIDAUDIO_OpenDevice;
  117. impl->PlayDevice = ANDROIDAUDIO_PlayDevice;
  118. impl->GetDeviceBuf = ANDROIDAUDIO_GetDeviceBuf;
  119. impl->CloseDevice = ANDROIDAUDIO_CloseDevice;
  120. impl->CaptureFromDevice = ANDROIDAUDIO_CaptureFromDevice;
  121. impl->FlushCapture = ANDROIDAUDIO_FlushCapture;
  122. /* and the capabilities */
  123. impl->HasCaptureSupport = SDL_TRUE;
  124. impl->OnlyHasDefaultOutputDevice = 1;
  125. impl->OnlyHasDefaultCaptureDevice = 1;
  126. return 1; /* this audio target is available. */
  127. }
  128. AudioBootStrap ANDROIDAUDIO_bootstrap = {
  129. "android", "SDL Android audio driver", ANDROIDAUDIO_Init, 0
  130. };
  131. /* Pause (block) all non already paused audio devices by taking their mixer lock */
  132. void ANDROIDAUDIO_PauseDevices(void)
  133. {
  134. /* TODO: Handle multiple devices? */
  135. struct SDL_PrivateAudioData *private;
  136. if(audioDevice != NULL && audioDevice->hidden != NULL) {
  137. private = (struct SDL_PrivateAudioData *) audioDevice->hidden;
  138. if (SDL_AtomicGet(&audioDevice->paused)) {
  139. /* The device is already paused, leave it alone */
  140. private->resume = SDL_FALSE;
  141. }
  142. else {
  143. SDL_LockMutex(audioDevice->mixer_lock);
  144. SDL_AtomicSet(&audioDevice->paused, 1);
  145. private->resume = SDL_TRUE;
  146. }
  147. }
  148. if(captureDevice != NULL && captureDevice->hidden != NULL) {
  149. private = (struct SDL_PrivateAudioData *) captureDevice->hidden;
  150. if (SDL_AtomicGet(&captureDevice->paused)) {
  151. /* The device is already paused, leave it alone */
  152. private->resume = SDL_FALSE;
  153. }
  154. else {
  155. SDL_LockMutex(captureDevice->mixer_lock);
  156. SDL_AtomicSet(&captureDevice->paused, 1);
  157. private->resume = SDL_TRUE;
  158. }
  159. }
  160. }
  161. /* Resume (unblock) all non already paused audio devices by releasing their mixer lock */
  162. void ANDROIDAUDIO_ResumeDevices(void)
  163. {
  164. /* TODO: Handle multiple devices? */
  165. struct SDL_PrivateAudioData *private;
  166. if(audioDevice != NULL && audioDevice->hidden != NULL) {
  167. private = (struct SDL_PrivateAudioData *) audioDevice->hidden;
  168. if (private->resume) {
  169. SDL_AtomicSet(&audioDevice->paused, 0);
  170. private->resume = SDL_FALSE;
  171. SDL_UnlockMutex(audioDevice->mixer_lock);
  172. }
  173. }
  174. if(captureDevice != NULL && captureDevice->hidden != NULL) {
  175. private = (struct SDL_PrivateAudioData *) captureDevice->hidden;
  176. if (private->resume) {
  177. SDL_AtomicSet(&captureDevice->paused, 0);
  178. private->resume = SDL_FALSE;
  179. SDL_UnlockMutex(captureDevice->mixer_lock);
  180. }
  181. }
  182. }
  183. #endif /* SDL_AUDIO_DRIVER_ANDROID */
  184. /* vi: set ts=4 sw=4 expandtab: */