audio_driver_opensl.cpp 12 KB

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