audio_driver_jandroid.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 "globals.h"
  32. #include "os/os.h"
  33. #include "thread_jandroid.h"
  34. #ifndef ANDROID_NATIVE_ACTIVITY
  35. AudioDriverAndroid *AudioDriverAndroid::s_ad = NULL;
  36. jobject AudioDriverAndroid::io;
  37. jmethodID AudioDriverAndroid::_init_audio;
  38. jmethodID AudioDriverAndroid::_write_buffer;
  39. jmethodID AudioDriverAndroid::_quit;
  40. jmethodID AudioDriverAndroid::_pause;
  41. bool AudioDriverAndroid::active = false;
  42. jclass AudioDriverAndroid::cls;
  43. int AudioDriverAndroid::audioBufferFrames = 0;
  44. int AudioDriverAndroid::mix_rate = 44100;
  45. bool AudioDriverAndroid::quit = false;
  46. jobject AudioDriverAndroid::audioBuffer = NULL;
  47. void *AudioDriverAndroid::audioBufferPinned = NULL;
  48. Mutex *AudioDriverAndroid::mutex = NULL;
  49. int32_t *AudioDriverAndroid::audioBuffer32 = NULL;
  50. const char *AudioDriverAndroid::get_name() const {
  51. return "Android";
  52. }
  53. Error AudioDriverAndroid::init() {
  54. mutex = Mutex::create();
  55. JNIEnv *env = ThreadAndroid::get_env();
  56. int mix_rate = GLOBAL_DEF("audio/mix_rate", 44100);
  57. int latency = GLOBAL_DEF("audio/output_latency", 25);
  58. latency = 50;
  59. unsigned int buffer_size = closest_power_of_2(latency * mix_rate / 1000);
  60. if (OS::get_singleton()->is_stdout_verbose()) {
  61. print_line("audio buffer size: " + itos(buffer_size));
  62. }
  63. audioBuffer = env->CallObjectMethod(io, _init_audio, mix_rate, buffer_size);
  64. ERR_FAIL_COND_V(audioBuffer == NULL, ERR_INVALID_PARAMETER);
  65. audioBuffer = env->NewGlobalRef(audioBuffer);
  66. jboolean isCopy = JNI_FALSE;
  67. audioBufferPinned = env->GetShortArrayElements((jshortArray)audioBuffer, &isCopy);
  68. audioBufferFrames = env->GetArrayLength((jshortArray)audioBuffer);
  69. audioBuffer32 = memnew_arr(int32_t, audioBufferFrames);
  70. return OK;
  71. }
  72. void AudioDriverAndroid::start() {
  73. active = true;
  74. }
  75. void AudioDriverAndroid::setup(jobject p_io) {
  76. JNIEnv *env = ThreadAndroid::get_env();
  77. io = p_io;
  78. jclass c = env->GetObjectClass(io);
  79. cls = (jclass)env->NewGlobalRef(c);
  80. _init_audio = env->GetMethodID(cls, "audioInit", "(II)Ljava/lang/Object;");
  81. _write_buffer = env->GetMethodID(cls, "audioWriteShortBuffer", "([S)V");
  82. _quit = env->GetMethodID(cls, "audioQuit", "()V");
  83. _pause = env->GetMethodID(cls, "audioPause", "(Z)V");
  84. }
  85. void AudioDriverAndroid::thread_func(JNIEnv *env) {
  86. jclass cls = env->FindClass("org/godotengine/godot/Godot");
  87. if (cls) {
  88. cls = (jclass)env->NewGlobalRef(cls);
  89. }
  90. jfieldID fid = env->GetStaticFieldID(cls, "io", "Lorg/godotengine/godot/GodotIO;");
  91. jobject ob = env->GetStaticObjectField(cls, fid);
  92. jobject gob = env->NewGlobalRef(ob);
  93. jclass c = env->GetObjectClass(gob);
  94. jclass lcls = (jclass)env->NewGlobalRef(c);
  95. _write_buffer = env->GetMethodID(lcls, "audioWriteShortBuffer", "([S)V");
  96. while (!quit) {
  97. int16_t *ptr = (int16_t *)audioBufferPinned;
  98. int fc = audioBufferFrames;
  99. if (!s_ad->active || mutex->try_lock() != OK) {
  100. for (int i = 0; i < fc; i++) {
  101. ptr[i] = 0;
  102. }
  103. } else {
  104. s_ad->audio_server_process(fc / 2, audioBuffer32);
  105. mutex->unlock();
  106. for (int i = 0; i < fc; i++) {
  107. ptr[i] = audioBuffer32[i] >> 16;
  108. }
  109. }
  110. env->ReleaseShortArrayElements((jshortArray)audioBuffer, (jshort *)ptr, JNI_COMMIT);
  111. env->CallVoidMethod(gob, _write_buffer, (jshortArray)audioBuffer);
  112. }
  113. }
  114. int AudioDriverAndroid::get_mix_rate() const {
  115. return mix_rate;
  116. }
  117. AudioDriverSW::OutputFormat AudioDriverAndroid::get_output_format() const {
  118. return OUTPUT_STEREO;
  119. }
  120. void AudioDriverAndroid::lock() {
  121. if (mutex)
  122. mutex->lock();
  123. }
  124. void AudioDriverAndroid::unlock() {
  125. if (mutex)
  126. mutex->unlock();
  127. }
  128. void AudioDriverAndroid::finish() {
  129. JNIEnv *env = ThreadAndroid::get_env();
  130. env->CallVoidMethod(io, _quit);
  131. if (audioBuffer) {
  132. env->DeleteGlobalRef(audioBuffer);
  133. audioBuffer = NULL;
  134. audioBufferPinned = NULL;
  135. }
  136. active = false;
  137. }
  138. void AudioDriverAndroid::set_pause(bool p_pause) {
  139. JNIEnv *env = ThreadAndroid::get_env();
  140. env->CallVoidMethod(io, _pause, p_pause);
  141. }
  142. AudioDriverAndroid::AudioDriverAndroid() {
  143. s_ad = this;
  144. active = false;
  145. }
  146. #endif