| 1 | // Copyright 2013 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #ifdef HAVE_OPENSL_ES |
| 5 | #include "AudioCommon/OpenSLESStream.h" |
| 6 | |
| 7 | #include <cmath> |
| 8 | |
| 9 | #include <SLES/OpenSLES.h> |
| 10 | #include <SLES/OpenSLES_Android.h> |
| 11 | #include <jni.h> |
| 12 | |
| 13 | #include "Common/Assert.h" |
| 14 | #include "Common/CommonTypes.h" |
| 15 | #include "Common/Logging/Log.h" |
| 16 | #include "Core/ConfigManager.h" |
| 17 | #include "jni/AndroidCommon/IDCache.h" |
| 18 | |
| 19 | void OpenSLESStream::BQPlayerCallback(SLAndroidSimpleBufferQueueItf bq, void* context) |
| 20 | { |
| 21 | reinterpret_cast<OpenSLESStream*>(context)->PushSamples(bq); |
| 22 | } |
| 23 | |
| 24 | void OpenSLESStream::PushSamples(SLAndroidSimpleBufferQueueItf bq) |
| 25 | { |
| 26 | ASSERT(bq == m_bq_player_buffer_queue); |
| 27 | |
| 28 | // Render to the fresh buffer |
| 29 | m_mixer->Mix(m_buffer[m_current_buffer].data(), m_frames_per_buffer); |
| 30 | SLresult result = (*bq)->Enqueue(bq, m_buffer[m_current_buffer].data(), m_bytes_per_buffer); |
| 31 | m_current_buffer ^= 1; // Switch buffer |
| 32 | |
| 33 | // Comment from sample code: |
| 34 | // the most likely other result is SL_RESULT_BUFFER_INSUFFICIENT, |
| 35 | // which for this code example would indicate a programming error |
| 36 | ASSERT_MSG(AUDIO, SL_RESULT_SUCCESS == result, "Couldn't enqueue audio stream."); |
| 37 | } |
| 38 | |
| 39 | bool OpenSLESStream::Init() |
| 40 | { |
| 41 | JNIEnv* env = IDCache::GetEnvForThread(); |
| 42 | jclass audio_utils = IDCache::GetAudioUtilsClass(); |
| 43 | const SLuint32 sample_rate = |
| 44 | env->CallStaticIntMethod(audio_utils, IDCache::GetAudioUtilsGetSampleRate()); |
| 45 | m_frames_per_buffer = |
| 46 | env->CallStaticIntMethod(audio_utils, IDCache::GetAudioUtilsGetFramesPerBuffer()); |
| 47 | |
| 48 | INFO_LOG_FMT(AUDIO, "OpenSLES configuration: {} Hz, {} frames per buffer", sample_rate, |
| 49 | m_frames_per_buffer); |
| 50 | |
| 51 | constexpr SLuint32 channels = 2; |
| 52 | const SLuint32 samples_per_buffer = m_frames_per_buffer * channels; |
| 53 | m_bytes_per_buffer = m_frames_per_buffer * channels * sizeof(m_buffer[0][0]); |
| 54 | |
| 55 | for (std::vector<short>& buffer : m_buffer) |
| 56 | buffer.resize(samples_per_buffer); |
| 57 | |
| 58 | SLresult result; |
| 59 | // create engine |
| 60 | result = slCreateEngine(&m_engine_object, 0, nullptr, 0, nullptr, nullptr); |
| 61 | ASSERT(SL_RESULT_SUCCESS == result); |
| 62 | result = (*m_engine_object)->Realize(m_engine_object, SL_BOOLEAN_FALSE); |
| 63 | ASSERT(SL_RESULT_SUCCESS == result); |
| 64 | result = (*m_engine_object)->GetInterface(m_engine_object, SL_IID_ENGINE, &m_engine_engine); |
| 65 | ASSERT(SL_RESULT_SUCCESS == result); |
| 66 | result = (*m_engine_engine)->CreateOutputMix(m_engine_engine, &m_output_mix_object, 0, 0, 0); |
| 67 | ASSERT(SL_RESULT_SUCCESS == result); |
| 68 | result = (*m_output_mix_object)->Realize(m_output_mix_object, SL_BOOLEAN_FALSE); |
| 69 | ASSERT(SL_RESULT_SUCCESS == result); |
| 70 | |
| 71 | SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2}; |
| 72 | SLDataFormat_PCM format_pcm = { |
| 73 | SL_DATAFORMAT_PCM, channels, |
| 74 | sample_rate * 1000, SL_PCMSAMPLEFORMAT_FIXED_16, |
| 75 | SL_PCMSAMPLEFORMAT_FIXED_16, SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT, |
| 76 | SL_BYTEORDER_LITTLEENDIAN}; |
| 77 | |
| 78 | SLDataSource audioSrc = {&loc_bufq, &format_pcm}; |
| 79 | |
| 80 | // configure audio sink |
| 81 | SLDataLocator_OutputMix loc_outmix = {SL_DATALOCATOR_OUTPUTMIX, m_output_mix_object}; |
| 82 | SLDataSink audioSnk = {&loc_outmix, nullptr}; |
| 83 | |
| 84 | // create audio player |
| 85 | const SLInterfaceID ids[2] = {SL_IID_BUFFERQUEUE, SL_IID_VOLUME}; |
| 86 | const SLboolean req[2] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE}; |
| 87 | result = (*m_engine_engine) |
| 88 | ->CreateAudioPlayer(m_engine_engine, &m_bq_player_object, &audioSrc, &audioSnk, 2, |
| 89 | ids, req); |
| 90 | ASSERT(SL_RESULT_SUCCESS == result); |
| 91 | |
| 92 | result = (*m_bq_player_object)->Realize(m_bq_player_object, SL_BOOLEAN_FALSE); |
| 93 | ASSERT(SL_RESULT_SUCCESS == result); |
| 94 | result = (*m_bq_player_object)->GetInterface(m_bq_player_object, SL_IID_PLAY, &m_bq_player_play); |
| 95 | ASSERT(SL_RESULT_SUCCESS == result); |
| 96 | result = (*m_bq_player_object) |
| 97 | ->GetInterface(m_bq_player_object, SL_IID_BUFFERQUEUE, &m_bq_player_buffer_queue); |
| 98 | ASSERT(SL_RESULT_SUCCESS == result); |
| 99 | result = |
| 100 | (*m_bq_player_object)->GetInterface(m_bq_player_object, SL_IID_VOLUME, &m_bq_player_volume); |
| 101 | ASSERT(SL_RESULT_SUCCESS == result); |
| 102 | result = (*m_bq_player_buffer_queue) |
| 103 | ->RegisterCallback(m_bq_player_buffer_queue, BQPlayerCallback, this); |
| 104 | ASSERT(SL_RESULT_SUCCESS == result); |
| 105 | result = (*m_bq_player_play)->SetPlayState(m_bq_player_play, SL_PLAYSTATE_PLAYING); |
| 106 | ASSERT(SL_RESULT_SUCCESS == result); |
| 107 | |
| 108 | // Render and enqueue a first buffer. |
| 109 | m_current_buffer ^= 1; |
| 110 | |
| 111 | result = (*m_bq_player_buffer_queue) |
| 112 | ->Enqueue(m_bq_player_buffer_queue, m_buffer[0].data(), m_bytes_per_buffer); |
| 113 | if (SL_RESULT_SUCCESS != result) |
| 114 | return false; |
| 115 | |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | OpenSLESStream::~OpenSLESStream() |
| 120 | { |
| 121 | if (m_bq_player_object != nullptr) |
| 122 | { |
| 123 | (*m_bq_player_object)->Destroy(m_bq_player_object); |
| 124 | m_bq_player_object = nullptr; |
| 125 | m_bq_player_play = nullptr; |
| 126 | m_bq_player_buffer_queue = nullptr; |
| 127 | m_bq_player_volume = nullptr; |
| 128 | } |
| 129 | |
| 130 | if (m_output_mix_object != nullptr) |
| 131 | { |
| 132 | (*m_output_mix_object)->Destroy(m_output_mix_object); |
| 133 | m_output_mix_object = nullptr; |
| 134 | } |
| 135 | |
| 136 | if (m_engine_object != nullptr) |
| 137 | { |
| 138 | (*m_engine_object)->Destroy(m_engine_object); |
| 139 | m_engine_object = nullptr; |
| 140 | m_engine_engine = nullptr; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | bool OpenSLESStream::SetRunning(bool running) |
| 145 | { |
| 146 | SLuint32 new_state = running ? SL_PLAYSTATE_PLAYING : SL_PLAYSTATE_PAUSED; |
| 147 | return (*m_bq_player_play)->SetPlayState(m_bq_player_play, new_state) == SL_RESULT_SUCCESS; |
| 148 | } |
| 149 | |
| 150 | void OpenSLESStream::SetVolume(int volume) |
| 151 | { |
| 152 | const SLmillibel attenuation = |
| 153 | volume <= 0 ? SL_MILLIBEL_MIN : static_cast<SLmillibel>(2000 * std::log10(volume / 100.0f)); |
| 154 | (*m_bq_player_volume)->SetVolumeLevel(m_bq_player_volume, attenuation); |
| 155 | } |
| 156 | #endif // HAVE_OPENSL_ES |