audio_driver_jandroid.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*************************************************************************/
  2. /* audio_driver_jandroid.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "audio_driver_jandroid.h"
  31. #include "core/os/os.h"
  32. #include "core/project_settings.h"
  33. #include "thread_jandroid.h"
  34. AudioDriverAndroid *AudioDriverAndroid::s_ad = NULL;
  35. jobject AudioDriverAndroid::io;
  36. jmethodID AudioDriverAndroid::_init_audio;
  37. jmethodID AudioDriverAndroid::_write_buffer;
  38. jmethodID AudioDriverAndroid::_quit;
  39. jmethodID AudioDriverAndroid::_pause;
  40. bool AudioDriverAndroid::active = false;
  41. jclass AudioDriverAndroid::cls;
  42. int AudioDriverAndroid::audioBufferFrames = 0;
  43. int AudioDriverAndroid::mix_rate = 44100;
  44. bool AudioDriverAndroid::quit = false;
  45. jobject AudioDriverAndroid::audioBuffer = NULL;
  46. void *AudioDriverAndroid::audioBufferPinned = NULL;
  47. Mutex *AudioDriverAndroid::mutex = NULL;
  48. int32_t *AudioDriverAndroid::audioBuffer32 = NULL;
  49. const char *AudioDriverAndroid::get_name() const {
  50. return "Android";
  51. }
  52. Error AudioDriverAndroid::init() {
  53. mutex = Mutex::create();
  54. /*
  55. // TODO: pass in/return a (Java) device ID, also whether we're opening for input or output
  56. this->spec.samples = Android_JNI_OpenAudioDevice(this->spec.freq, this->spec.format == AUDIO_U8 ? 0 : 1, this->spec.channels, this->spec.samples);
  57. SDL_CalculateAudioSpec(&this->spec);
  58. if (this->spec.samples == 0) {
  59. // Init failed?
  60. SDL_SetError("Java-side initialization failed!");
  61. return 0;
  62. }
  63. */
  64. //Android_JNI_SetupThread();
  65. // __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "SDL audio: opening device");
  66. JNIEnv *env = ThreadAndroid::get_env();
  67. int mix_rate = GLOBAL_DEF_RST("audio/mix_rate", 44100);
  68. int latency = GLOBAL_DEF_RST("audio/output_latency", 25);
  69. unsigned int buffer_size = next_power_of_2(latency * mix_rate / 1000);
  70. print_verbose("Audio buffer size: " + itos(buffer_size));
  71. audioBuffer = env->CallObjectMethod(io, _init_audio, mix_rate, buffer_size);
  72. ERR_FAIL_COND_V(audioBuffer == NULL, ERR_INVALID_PARAMETER);
  73. audioBuffer = env->NewGlobalRef(audioBuffer);
  74. jboolean isCopy = JNI_FALSE;
  75. audioBufferPinned = env->GetShortArrayElements((jshortArray)audioBuffer, &isCopy);
  76. audioBufferFrames = env->GetArrayLength((jshortArray)audioBuffer);
  77. audioBuffer32 = memnew_arr(int32_t, audioBufferFrames);
  78. return OK;
  79. }
  80. void AudioDriverAndroid::start() {
  81. active = true;
  82. }
  83. void AudioDriverAndroid::setup(jobject p_io) {
  84. JNIEnv *env = ThreadAndroid::get_env();
  85. io = p_io;
  86. jclass c = env->GetObjectClass(io);
  87. cls = (jclass)env->NewGlobalRef(c);
  88. _init_audio = env->GetMethodID(cls, "audioInit", "(II)Ljava/lang/Object;");
  89. _write_buffer = env->GetMethodID(cls, "audioWriteShortBuffer", "([S)V");
  90. _quit = env->GetMethodID(cls, "audioQuit", "()V");
  91. _pause = env->GetMethodID(cls, "audioPause", "(Z)V");
  92. }
  93. void AudioDriverAndroid::thread_func(JNIEnv *env) {
  94. jclass cls = env->FindClass("org/godotengine/godot/Godot");
  95. if (cls) {
  96. cls = (jclass)env->NewGlobalRef(cls);
  97. }
  98. jfieldID fid = env->GetStaticFieldID(cls, "io", "Lorg/godotengine/godot/GodotIO;");
  99. jobject ob = env->GetStaticObjectField(cls, fid);
  100. jobject gob = env->NewGlobalRef(ob);
  101. jclass c = env->GetObjectClass(gob);
  102. jclass lcls = (jclass)env->NewGlobalRef(c);
  103. _write_buffer = env->GetMethodID(lcls, "audioWriteShortBuffer", "([S)V");
  104. while (!quit) {
  105. int16_t *ptr = (int16_t *)audioBufferPinned;
  106. int fc = audioBufferFrames;
  107. if (!s_ad->active || mutex->try_lock() != OK) {
  108. for (int i = 0; i < fc; i++) {
  109. ptr[i] = 0;
  110. }
  111. } else {
  112. s_ad->audio_server_process(fc / 2, audioBuffer32);
  113. mutex->unlock();
  114. for (int i = 0; i < fc; i++) {
  115. ptr[i] = audioBuffer32[i] >> 16;
  116. }
  117. }
  118. env->ReleaseShortArrayElements((jshortArray)audioBuffer, (jshort *)ptr, JNI_COMMIT);
  119. env->CallVoidMethod(gob, _write_buffer, (jshortArray)audioBuffer);
  120. }
  121. }
  122. int AudioDriverAndroid::get_mix_rate() const {
  123. return mix_rate;
  124. }
  125. AudioDriver::SpeakerMode AudioDriverAndroid::get_speaker_mode() const {
  126. return SPEAKER_MODE_STEREO;
  127. }
  128. void AudioDriverAndroid::lock() {
  129. if (mutex)
  130. mutex->lock();
  131. }
  132. void AudioDriverAndroid::unlock() {
  133. if (mutex)
  134. mutex->unlock();
  135. }
  136. void AudioDriverAndroid::finish() {
  137. JNIEnv *env = ThreadAndroid::get_env();
  138. env->CallVoidMethod(io, _quit);
  139. if (audioBuffer) {
  140. env->DeleteGlobalRef(audioBuffer);
  141. audioBuffer = NULL;
  142. audioBufferPinned = NULL;
  143. }
  144. active = false;
  145. }
  146. void AudioDriverAndroid::set_pause(bool p_pause) {
  147. JNIEnv *env = ThreadAndroid::get_env();
  148. env->CallVoidMethod(io, _pause, p_pause);
  149. }
  150. AudioDriverAndroid::AudioDriverAndroid() {
  151. s_ad = this;
  152. active = false;
  153. }