SDL_androidaudio.c 6.1 KB

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