| 6 |
| 7 | #include "../../logging.h" |
| 8 | #include "../../VoIPController.h" |
| 9 | #include "AudioOutputOpenSLES.h" |
| 10 | #include "AudioInputAndroid.h" |
| 11 | #include "OpenSLEngineWrapper.h" |
| 12 |
| 13 | #include <sys/time.h> |
| 14 | #include <unistd.h> |
| 15 |
| 16 | #include <cassert> |
| 17 | #include <cstring> |
| 18 |
| 19 | #define CHECK_SL_ERROR(res, msg) \ |
| 20 | if (res != SL_RESULT_SUCCESS) \ |
| 21 | { \ |
| 22 | LOGE(msg); \ |
| 23 | m_failed = true; \ |
| 24 | return; \ |
| 25 | } |
| 26 | #define BUFFER_SIZE 960 // 20 ms |
| 27 |
| 28 | using namespace tgvoip; |
| 29 | using namespace tgvoip::audio; |
| 30 |
| 31 | unsigned int AudioOutputOpenSLES::nativeBufferSize; |
| 32 |
| 33 | AudioOutputOpenSLES::AudioOutputOpenSLES() |
| 34 | { |
| 35 | SLresult result; |
| 36 | slEngine = OpenSLEngineWrapper::CreateEngine(); |
| 37 |
| 38 | const SLInterfaceID pOutputMixIDs[] = {}; |
| 39 | const SLboolean pOutputMixRequired[] = {}; |
| 40 | result = (*slEngine)->CreateOutputMix(slEngine, &slOutputMixObj, 0, pOutputMixIDs, pOutputMixRequired); |
| 41 | CHECK_SL_ERROR(result, "Error creating output mix"); |
| 42 |
| 43 | result = (*slOutputMixObj)->Realize(slOutputMixObj, SL_BOOLEAN_FALSE); |
| 44 | CHECK_SL_ERROR(result, "Error realizing output mix"); |
| 45 |
| 46 | LOGI("Native buffer size is %u samples", nativeBufferSize); |
| 47 | /*if(nativeBufferSize<BUFFER_SIZE && BUFFER_SIZE % nativeBufferSize!=0){ |
| 48 | LOGE("20ms is not divisible by native buffer size!!"); |
| 49 | nativeBufferSize=BUFFER_SIZE; |
| 50 | }else if(nativeBufferSize>BUFFER_SIZE && nativeBufferSize%BUFFER_SIZE!=0){ |
| 51 | LOGE("native buffer size is not multiple of 20ms!!"); |
| 52 | nativeBufferSize+=nativeBufferSize%BUFFER_SIZE; |
| 53 | } |
| 54 | LOGI("Adjusted native buffer size is %u", nativeBufferSize);*/ |
| 55 |
| 56 | buffer = reinterpret_cast<std::int16_t*>(std::calloc(BUFFER_SIZE, sizeof(std::int16_t))); |
| 57 | nativeBuffer = reinterpret_cast<std::int16_t*>(std::calloc(static_cast<std::size_t>(nativeBufferSize), sizeof(std::int16_t))); |
| 58 | slPlayerObj = nullptr; |
| 59 | remainingDataSize = 0; |
| 60 | } |
| 61 |
| 62 | AudioOutputOpenSLES::~AudioOutputOpenSLES() |
| 63 | { |
| 64 | if (!stopped) |
| 65 | Stop(); |
| 66 | (*slBufferQueue)->Clear(slBufferQueue); |
| 67 | LOGV("destroy slPlayerObj"); |
| 68 | (*slPlayerObj)->Destroy(slPlayerObj); |
| 69 | LOGV("destroy slOutputMixObj"); |
| 70 | (*slOutputMixObj)->Destroy(slOutputMixObj); |
| 71 | OpenSLEngineWrapper::DestroyEngine(); |
| 72 | std::free(buffer); |
| 73 | std::free(nativeBuffer); |
| 74 | } |
| 75 |
| 76 | void AudioOutputOpenSLES::SetNativeBufferSize(unsigned int size) |
| 77 | { |
| 78 | AudioOutputOpenSLES::nativeBufferSize = size; |
| 79 | } |
| 80 |
| 81 | void AudioOutputOpenSLES::BufferCallback(SLAndroidSimpleBufferQueueItf bq, void* context) |
| 82 | { |
| 83 | reinterpret_cast<AudioOutputOpenSLES*>(context)->HandleSLCallback(); |
| 84 | } |
| 85 |
| 86 | void AudioOutputOpenSLES::Configure(std::uint32_t sampleRate, std::uint32_t bitsPerSample, std::uint32_t channels) |
| 87 | { |
| 88 | assert(slPlayerObj == nullptr); |
| 89 | SLDataLocator_AndroidSimpleBufferQueue locatorBufferQueue = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 1}; |
| 90 | SLDataFormat_PCM formatPCM = {SL_DATAFORMAT_PCM, channels, sampleRate * 1000, |
| 91 | SL_PCMSAMPLEFORMAT_FIXED_16, SL_PCMSAMPLEFORMAT_FIXED_16, |
| 92 | channels == 2 ? (SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT) : SL_SPEAKER_FRONT_CENTER, SL_BYTEORDER_LITTLEENDIAN}; |
| 93 | SLDataSource audioSrc = {&locatorBufferQueue, &formatPCM}; |
| 94 | SLDataLocator_OutputMix locatorOutMix = {SL_DATALOCATOR_OUTPUTMIX, slOutputMixObj}; |
| 95 | SLDataSink audioSnk = {&locatorOutMix, nullptr}; |
| 96 |
| 97 | const SLInterfaceID id[2] = {SL_IID_BUFFERQUEUE, SL_IID_ANDROIDCONFIGURATION}; |
| 98 | const SLboolean req[2] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE}; |
| 99 | SLresult result = (*slEngine)->CreateAudioPlayer(slEngine, &slPlayerObj, &audioSrc, &audioSnk, 2, id, req); |
| 100 | CHECK_SL_ERROR(result, "Error creating player"); |
| 101 |
| 102 | SLAndroidConfigurationItf playerConfig; |
| 103 | result = (*slPlayerObj)->GetInterface(slPlayerObj, SL_IID_ANDROIDCONFIGURATION, &playerConfig); |
| 104 | SLint32 streamType = SL_ANDROID_STREAM_VOICE; |
| 105 | result = (*playerConfig)->SetConfiguration(playerConfig, SL_ANDROID_KEY_STREAM_TYPE, &streamType, sizeof(SLint32)); |
| 106 |
| 107 | result = (*slPlayerObj)->Realize(slPlayerObj, SL_BOOLEAN_FALSE); |
| 108 | CHECK_SL_ERROR(result, "Error realizing player"); |
| 109 |
| 110 | result = (*slPlayerObj)->GetInterface(slPlayerObj, SL_IID_PLAY, &slPlayer); |
| 111 | CHECK_SL_ERROR(result, "Error getting player interface"); |
| 112 |
| 113 | result = (*slPlayerObj)->GetInterface(slPlayerObj, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, &slBufferQueue); |
| 114 | CHECK_SL_ERROR(result, "Error getting buffer queue"); |
| 115 |
| 116 | result = (*slBufferQueue)->RegisterCallback(slBufferQueue, AudioOutputOpenSLES::BufferCallback, this); |
| 117 | CHECK_SL_ERROR(result, "Error setting buffer queue callback"); |
| 118 |
| 119 | (*slBufferQueue)->Enqueue(slBufferQueue, nativeBuffer, nativeBufferSize * sizeof(std::int16_t)); |
| 120 | } |
| 121 |
| 122 | bool AudioOutputOpenSLES::IsPhone() |
| 123 | { |
| 124 | return false; |
| 125 | } |
| 126 |
| 127 | void AudioOutputOpenSLES::EnableLoudspeaker(bool enabled) |
| 128 | { |
| 129 | } |
| 130 |
| 131 | void AudioOutputOpenSLES::Start() |
| 132 | { |
| 133 | stopped = false; |
| 134 | SLresult result = (*slPlayer)->SetPlayState(slPlayer, SL_PLAYSTATE_PLAYING); |
| 135 | CHECK_SL_ERROR(result, "Error starting player"); |
| 136 | } |
| 137 |
| 138 | void AudioOutputOpenSLES::Stop() |
| 139 | { |
| 140 | stopped = true; |
| 141 | LOGV("Stopping OpenSL output"); |
| 142 | SLresult result = (*slPlayer)->SetPlayState(slPlayer, SL_PLAYSTATE_PAUSED); |
| 143 | CHECK_SL_ERROR(result, "Error starting player"); |
| 144 | } |
| 145 |
| 146 | void AudioOutputOpenSLES::HandleSLCallback() |
| 147 | { |
| 148 | if (!stopped) |
| 149 | { |
| 150 | while (remainingDataSize < nativeBufferSize * 2) |
| 151 | { |
| 152 | assert(remainingDataSize + BUFFER_SIZE * 2 < 10240); |
| 153 | InvokeCallback(remainingData + remainingDataSize, BUFFER_SIZE * 2); |
| 154 | remainingDataSize += BUFFER_SIZE * 2; |
| 155 | } |
| 156 | std::memcpy(nativeBuffer, remainingData, nativeBufferSize * 2); |
| 157 | remainingDataSize -= nativeBufferSize * 2; |
| 158 | if (remainingDataSize > 0) |
| 159 | memmove(remainingData, remainingData + nativeBufferSize * 2, remainingDataSize); |
| 160 | } |
| 161 | else |
| 162 | { |
| 163 | std::memset(nativeBuffer, 0, nativeBufferSize * 2); |
| 164 | } |
| 165 |
| 166 | (*slBufferQueue)->Enqueue(slBufferQueue, nativeBuffer, nativeBufferSize * sizeof(std::int16_t)); |
| 167 | } |
| 168 |
| 169 | bool AudioOutputOpenSLES::IsPlaying() |
| 170 | { |
| 171 | if (slPlayer) |
| 172 | { |
| 173 | std::uint32_t state; |
| 174 | (*slPlayer)->GetPlayState(slPlayer, &state); |
| 175 | return state == SL_PLAYSTATE_PLAYING; |
| 176 | } |
| 177 | return false; |
| 178 | } |
| 179 |
| 180 | float AudioOutputOpenSLES::GetLevel() |
| 181 | { |
| 182 | return 0; // we don't use this anyway |
| 183 | } |
| 184 |