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