audio_driver_opensl.cpp 12 KB

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