audio_driver_opensl.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /**************************************************************************/
  2. /* audio_driver_opensl.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_opensl.h"
  31. #include <string.h>
  32. #define MAX_NUMBER_INTERFACES 3
  33. #define MAX_NUMBER_OUTPUT_DEVICES 6
  34. /* Structure for passing information to callback function */
  35. void AudioDriverOpenSL::_buffer_callback(
  36. SLAndroidSimpleBufferQueueItf queueItf) {
  37. bool mix = true;
  38. if (pause) {
  39. mix = false;
  40. } else {
  41. mix = mutex.try_lock();
  42. }
  43. if (mix) {
  44. audio_server_process(buffer_size, mixdown_buffer);
  45. } else {
  46. int32_t *src_buff = mixdown_buffer;
  47. for (unsigned int i = 0; i < buffer_size * 2; i++) {
  48. src_buff[i] = 0;
  49. }
  50. }
  51. if (mix) {
  52. mutex.unlock();
  53. }
  54. const int32_t *src_buff = mixdown_buffer;
  55. int16_t *ptr = (int16_t *)buffers[last_free];
  56. last_free = (last_free + 1) % BUFFER_COUNT;
  57. for (unsigned int i = 0; i < buffer_size * 2; i++) {
  58. ptr[i] = src_buff[i] >> 16;
  59. }
  60. (*queueItf)->Enqueue(queueItf, ptr, 4 * buffer_size);
  61. }
  62. void AudioDriverOpenSL::_buffer_callbacks(
  63. SLAndroidSimpleBufferQueueItf queueItf,
  64. void *pContext) {
  65. AudioDriverOpenSL *ad = static_cast<AudioDriverOpenSL *>(pContext);
  66. ad->_buffer_callback(queueItf);
  67. }
  68. Error AudioDriverOpenSL::init() {
  69. SLresult res;
  70. SLEngineOption EngineOption[] = {
  71. { (SLuint32)SL_ENGINEOPTION_THREADSAFE, (SLuint32)SL_BOOLEAN_TRUE }
  72. };
  73. res = slCreateEngine(&sl, 1, EngineOption, 0, nullptr, nullptr);
  74. ERR_FAIL_COND_V_MSG(res != SL_RESULT_SUCCESS, ERR_INVALID_PARAMETER, "Could not initialize OpenSL.");
  75. res = (*sl)->Realize(sl, SL_BOOLEAN_FALSE);
  76. ERR_FAIL_COND_V_MSG(res != SL_RESULT_SUCCESS, ERR_INVALID_PARAMETER, "Could not realize OpenSL.");
  77. return OK;
  78. }
  79. void AudioDriverOpenSL::start() {
  80. active = false;
  81. SLresult res;
  82. buffer_size = 1024;
  83. for (int i = 0; i < BUFFER_COUNT; i++) {
  84. buffers[i] = memnew_arr(int16_t, buffer_size * 2);
  85. memset(buffers[i], 0, buffer_size * 4);
  86. }
  87. mixdown_buffer = memnew_arr(int32_t, buffer_size * 2);
  88. /* Callback context for the buffer queue callback function */
  89. /* Get the SL Engine Interface which is implicit */
  90. res = (*sl)->GetInterface(sl, SL_IID_ENGINE, (void *)&EngineItf);
  91. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  92. {
  93. const SLInterfaceID ids[1] = { SL_IID_ENVIRONMENTALREVERB };
  94. const SLboolean req[1] = { SL_BOOLEAN_FALSE };
  95. res = (*EngineItf)->CreateOutputMix(EngineItf, &OutputMix, 0, ids, req);
  96. }
  97. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  98. // Realizing the Output Mix object in synchronous mode.
  99. res = (*OutputMix)->Realize(OutputMix, SL_BOOLEAN_FALSE);
  100. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  101. SLDataLocator_AndroidSimpleBufferQueue loc_bufq = { SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, BUFFER_COUNT };
  102. /* Setup the format of the content in the buffer queue */
  103. pcm.formatType = SL_DATAFORMAT_PCM;
  104. pcm.numChannels = 2;
  105. pcm.samplesPerSec = SL_SAMPLINGRATE_44_1;
  106. pcm.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16;
  107. pcm.containerSize = SL_PCMSAMPLEFORMAT_FIXED_16;
  108. pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
  109. #ifdef BIG_ENDIAN_ENABLED
  110. pcm.endianness = SL_BYTEORDER_BIGENDIAN;
  111. #else
  112. pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;
  113. #endif
  114. audioSource.pFormat = (void *)&pcm;
  115. audioSource.pLocator = (void *)&loc_bufq;
  116. /* Setup the data sink structure */
  117. locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
  118. locator_outputmix.outputMix = OutputMix;
  119. audioSink.pLocator = (void *)&locator_outputmix;
  120. audioSink.pFormat = nullptr;
  121. /* Create the music player */
  122. {
  123. const SLInterfaceID ids[2] = { SL_IID_BUFFERQUEUE, SL_IID_EFFECTSEND };
  124. const SLboolean req[2] = { SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE };
  125. res = (*EngineItf)->CreateAudioPlayer(EngineItf, &player, &audioSource, &audioSink, 1, ids, req);
  126. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  127. }
  128. /* Realizing the player in synchronous mode. */
  129. res = (*player)->Realize(player, SL_BOOLEAN_FALSE);
  130. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  131. /* Get seek and play interfaces */
  132. res = (*player)->GetInterface(player, SL_IID_PLAY, (void *)&playItf);
  133. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  134. res = (*player)->GetInterface(player, SL_IID_BUFFERQUEUE,
  135. (void *)&bufferQueueItf);
  136. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  137. /* Setup to receive buffer queue event callbacks */
  138. res = (*bufferQueueItf)->RegisterCallback(bufferQueueItf, _buffer_callbacks, this);
  139. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  140. last_free = 0;
  141. //fill up buffers
  142. for (int i = 0; i < BUFFER_COUNT; i++) {
  143. /* Enqueue a few buffers to get the ball rolling */
  144. res = (*bufferQueueItf)->Enqueue(bufferQueueItf, buffers[i], 4 * buffer_size); /* Size given in */
  145. }
  146. res = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING);
  147. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  148. active = true;
  149. }
  150. void AudioDriverOpenSL::_record_buffer_callback(SLAndroidSimpleBufferQueueItf queueItf) {
  151. for (int i = 0; i < rec_buffer.size(); i++) {
  152. int32_t sample = rec_buffer[i] << 16;
  153. input_buffer_write(sample);
  154. input_buffer_write(sample); // call twice to convert to Stereo
  155. }
  156. SLresult res = (*recordBufferQueueItf)->Enqueue(recordBufferQueueItf, rec_buffer.ptrw(), rec_buffer.size() * sizeof(int16_t));
  157. ERR_FAIL_COND(res != SL_RESULT_SUCCESS);
  158. }
  159. void AudioDriverOpenSL::_record_buffer_callbacks(SLAndroidSimpleBufferQueueItf queueItf, void *pContext) {
  160. AudioDriverOpenSL *ad = static_cast<AudioDriverOpenSL *>(pContext);
  161. ad->_record_buffer_callback(queueItf);
  162. }
  163. Error AudioDriverOpenSL::init_input_device() {
  164. SLDataLocator_IODevice loc_dev = {
  165. SL_DATALOCATOR_IODEVICE,
  166. SL_IODEVICE_AUDIOINPUT,
  167. SL_DEFAULTDEVICEID_AUDIOINPUT,
  168. nullptr
  169. };
  170. SLDataSource recSource = { &loc_dev, nullptr };
  171. SLDataLocator_AndroidSimpleBufferQueue loc_bq = {
  172. SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE,
  173. 2
  174. };
  175. SLDataFormat_PCM format_pcm = {
  176. SL_DATAFORMAT_PCM,
  177. 1,
  178. SL_SAMPLINGRATE_44_1,
  179. SL_PCMSAMPLEFORMAT_FIXED_16,
  180. SL_PCMSAMPLEFORMAT_FIXED_16,
  181. SL_SPEAKER_FRONT_CENTER,
  182. SL_BYTEORDER_LITTLEENDIAN
  183. };
  184. SLDataSink recSnk = { &loc_bq, &format_pcm };
  185. const SLInterfaceID ids[2] = { SL_IID_ANDROIDSIMPLEBUFFERQUEUE, SL_IID_ANDROIDCONFIGURATION };
  186. const SLboolean req[2] = { SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE };
  187. SLresult res = (*EngineItf)->CreateAudioRecorder(EngineItf, &recorder, &recSource, &recSnk, 2, ids, req);
  188. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  189. res = (*recorder)->Realize(recorder, SL_BOOLEAN_FALSE);
  190. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  191. res = (*recorder)->GetInterface(recorder, SL_IID_RECORD, (void *)&recordItf);
  192. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  193. res = (*recorder)->GetInterface(recorder, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, (void *)&recordBufferQueueItf);
  194. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  195. res = (*recordBufferQueueItf)->RegisterCallback(recordBufferQueueItf, _record_buffer_callbacks, this);
  196. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  197. SLuint32 state;
  198. res = (*recordItf)->GetRecordState(recordItf, &state);
  199. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  200. if (state != SL_RECORDSTATE_STOPPED) {
  201. res = (*recordItf)->SetRecordState(recordItf, SL_RECORDSTATE_STOPPED);
  202. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  203. res = (*recordBufferQueueItf)->Clear(recordBufferQueueItf);
  204. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  205. }
  206. const int rec_buffer_frames = 2048;
  207. rec_buffer.resize(rec_buffer_frames);
  208. input_buffer_init(rec_buffer_frames);
  209. res = (*recordBufferQueueItf)->Enqueue(recordBufferQueueItf, rec_buffer.ptrw(), rec_buffer.size() * sizeof(int16_t));
  210. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  211. res = (*recordItf)->SetRecordState(recordItf, SL_RECORDSTATE_RECORDING);
  212. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  213. return OK;
  214. }
  215. Error AudioDriverOpenSL::input_start() {
  216. if (recordItf || recordBufferQueueItf) {
  217. return ERR_ALREADY_IN_USE;
  218. }
  219. if (OS::get_singleton()->request_permission("RECORD_AUDIO")) {
  220. return init_input_device();
  221. }
  222. WARN_PRINT("Unable to start audio capture - No RECORD_AUDIO permission");
  223. return ERR_UNAUTHORIZED;
  224. }
  225. Error AudioDriverOpenSL::input_stop() {
  226. if (!recordItf || !recordBufferQueueItf) {
  227. return ERR_CANT_OPEN;
  228. }
  229. SLuint32 state;
  230. SLresult res = (*recordItf)->GetRecordState(recordItf, &state);
  231. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  232. if (state != SL_RECORDSTATE_STOPPED) {
  233. res = (*recordItf)->SetRecordState(recordItf, SL_RECORDSTATE_STOPPED);
  234. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  235. res = (*recordBufferQueueItf)->Clear(recordBufferQueueItf);
  236. ERR_FAIL_COND_V(res != SL_RESULT_SUCCESS, ERR_CANT_OPEN);
  237. }
  238. return OK;
  239. }
  240. int AudioDriverOpenSL::get_mix_rate() const {
  241. return 44100; // hardcoded for Android, as selected by SL_SAMPLINGRATE_44_1
  242. }
  243. AudioDriver::SpeakerMode AudioDriverOpenSL::get_speaker_mode() const {
  244. return SPEAKER_MODE_STEREO;
  245. }
  246. void AudioDriverOpenSL::lock() {
  247. if (active) {
  248. mutex.lock();
  249. }
  250. }
  251. void AudioDriverOpenSL::unlock() {
  252. if (active) {
  253. mutex.unlock();
  254. }
  255. }
  256. void AudioDriverOpenSL::finish() {
  257. if (recordItf) {
  258. (*recordItf)->SetRecordState(recordItf, SL_RECORDSTATE_STOPPED);
  259. recordItf = nullptr;
  260. }
  261. if (recorder) {
  262. (*recorder)->Destroy(recorder);
  263. recorder = nullptr;
  264. }
  265. if (playItf) {
  266. (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED);
  267. playItf = nullptr;
  268. }
  269. if (player) {
  270. (*player)->Destroy(player);
  271. player = nullptr;
  272. }
  273. if (OutputMix) {
  274. (*OutputMix)->Destroy(OutputMix);
  275. OutputMix = nullptr;
  276. }
  277. if (sl) {
  278. (*sl)->Destroy(sl);
  279. sl = nullptr;
  280. }
  281. }
  282. void AudioDriverOpenSL::set_pause(bool p_pause) {
  283. pause = p_pause;
  284. if (active && playItf) {
  285. if (pause) {
  286. (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PAUSED);
  287. } else {
  288. (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING);
  289. }
  290. }
  291. }
  292. AudioDriverOpenSL::AudioDriverOpenSL() {
  293. }