|
| 1 | +/* |
| 2 | +* Copyright (c) 2018-present, lmyooyo@gmail.com. |
| 3 | +* |
| 4 | +* This source code is licensed under the GPL license found in the |
| 5 | +* LICENSE file in the root directory of this source tree. |
| 6 | +*/ |
| 7 | +#include "../include/AudioPlayer.h" |
| 8 | + |
| 9 | +AudioPlayer::AudioPlayer(int channels, int sampleHz) { |
| 10 | + this->channels = channels; |
| 11 | + this->sampleHz = sampleHz; |
| 12 | + this->quietSize = static_cast<size_t>(sampleHz / 1000 * 2); |
| 13 | + this->quietBuffer = static_cast<uint8_t *>(malloc(this->quietSize)); |
| 14 | + memset(this->quietBuffer, 0, this->quietSize); |
| 15 | + LOGI("Create AudioPlayer, channels=%d, sampleHz=%d, quietSize=%d", |
| 16 | + this->channels, |
| 17 | + this->sampleHz, |
| 18 | + this->quietSize); |
| 19 | + engineObject = nullptr; |
| 20 | + engineItf = nullptr; |
| 21 | + mixObject = nullptr; |
| 22 | + playObject = nullptr; |
| 23 | + playItf = nullptr; |
| 24 | +} |
| 25 | + |
| 26 | +AudioPlayer::~AudioPlayer() { |
| 27 | + LOGI("~AudioPlayer"); |
| 28 | + stop(); |
| 29 | +} |
| 30 | + |
| 31 | +int AudioPlayer::createEngine() { |
| 32 | + SLresult result = slCreateEngine(&engineObject, 0, NULL, 0, NULL, NULL); |
| 33 | + if (SL_RESULT_SUCCESS != result) { |
| 34 | + LOGE("slCreateEngine failed!"); |
| 35 | + return 0; |
| 36 | + } |
| 37 | + result = (*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE); |
| 38 | + if (SL_RESULT_SUCCESS != result) { |
| 39 | + LOGE("Engine Realize failed!"); |
| 40 | + return 0; |
| 41 | + } |
| 42 | + result = (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineItf); |
| 43 | + if (SL_RESULT_SUCCESS != result) { |
| 44 | + LOGE("Engine GetInterface failed!"); |
| 45 | + return 0; |
| 46 | + } |
| 47 | + |
| 48 | + result = (*engineItf)->CreateOutputMix(engineItf, &mixObject, 0, nullptr, nullptr); |
| 49 | + if (SL_RESULT_SUCCESS != result) { |
| 50 | + LOGE("CreateOutputMix failed!"); |
| 51 | + return 0; |
| 52 | + } |
| 53 | + result = (*mixObject)->Realize(mixObject, SL_BOOLEAN_FALSE); |
| 54 | + if (SL_RESULT_SUCCESS != result) { |
| 55 | + LOGE("OutputMix Realize failed!"); |
| 56 | + return 0; |
| 57 | + } |
| 58 | + return createBufferQueueAudioPlayer(); |
| 59 | +} |
| 60 | + |
| 61 | +int AudioPlayer::start() { |
| 62 | + int ret = createEngine(); |
| 63 | + if (!ret) { |
| 64 | + stop(); |
| 65 | + } |
| 66 | + return ret; |
| 67 | +} |
| 68 | + |
| 69 | +string AudioPlayer::getString() { |
| 70 | + return "Test"; |
| 71 | +} |
| 72 | + |
| 73 | +uint8_t *AudioPlayer::readBuffer(size_t *size) { |
| 74 | + if (!requestRead) { |
| 75 | + *size = this->quietSize; |
| 76 | + return this->quietBuffer; |
| 77 | + } |
| 78 | + requestRead = false; |
| 79 | + *size = this->size; |
| 80 | + return this->buffer; |
| 81 | +} |
| 82 | + |
| 83 | +static SLuint32 getChannelMask(int channels) { |
| 84 | + switch (channels) { |
| 85 | + case 1: |
| 86 | + return SL_SPEAKER_FRONT_CENTER; |
| 87 | + case 3: |
| 88 | + return SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT | SL_SPEAKER_FRONT_CENTER; |
| 89 | + case 2: |
| 90 | + default: |
| 91 | + return SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT; |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +int AudioPlayer::createBufferQueueAudioPlayer() { |
| 96 | + SLDataLocator_BufferQueue queue = {SL_DATALOCATOR_BUFFERQUEUE, 2}; |
| 97 | + SLDataFormat_PCM pcm = {SL_DATAFORMAT_PCM, |
| 98 | + channels, |
| 99 | + sampleHz * 1000, |
| 100 | + SL_PCMSAMPLEFORMAT_FIXED_16, |
| 101 | + SL_PCMSAMPLEFORMAT_FIXED_16, |
| 102 | + getChannelMask(channels), |
| 103 | + SL_BYTEORDER_LITTLEENDIAN}; |
| 104 | + SLDataSource dataSource = {&queue, &pcm}; |
| 105 | + SLDataLocator_OutputMix slDataLocator_outputMix = {SL_DATALOCATOR_OUTPUTMIX, mixObject}; |
| 106 | + SLDataSink slDataSink = {&slDataLocator_outputMix, NULL}; |
| 107 | + const SLInterfaceID ids[2] = {SL_IID_BUFFERQUEUE, SL_IID_VOLUME}; |
| 108 | + const SLboolean req[2] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE}; |
| 109 | + SLresult result = (*engineItf)->CreateAudioPlayer(engineItf, |
| 110 | + &playObject, |
| 111 | + &dataSource, |
| 112 | + &slDataSink, |
| 113 | + 2, |
| 114 | + ids, |
| 115 | + req); |
| 116 | + if (SL_RESULT_SUCCESS != result) { |
| 117 | + LOGE("CreateAudioPlayer failed! ret=%d", result); |
| 118 | + return 0; |
| 119 | + } |
| 120 | + result = (*playObject)->Realize(playObject, SL_BOOLEAN_FALSE); |
| 121 | + if (SL_RESULT_SUCCESS != result) { |
| 122 | + LOGE("Player Realize failed!"); |
| 123 | + return 0; |
| 124 | + } |
| 125 | + result = (*playObject)->GetInterface(playObject, SL_IID_PLAY, &playItf); |
| 126 | + if (SL_RESULT_SUCCESS != result) { |
| 127 | + LOGE("Player GetInterface failed!"); |
| 128 | + return 0; |
| 129 | + } |
| 130 | + result = (*playObject)->GetInterface(playObject, SL_IID_BUFFERQUEUE, &bufferQueueItf); |
| 131 | + if (SL_RESULT_SUCCESS != result) { |
| 132 | + LOGE("Player GetInterface buffer queue failed!"); |
| 133 | + return 0; |
| 134 | + } |
| 135 | + result = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_STOPPED); |
| 136 | + if (SL_RESULT_SUCCESS != result) { |
| 137 | + LOGE("Player SetPlayState stop failed!"); |
| 138 | + return 0; |
| 139 | + } |
| 140 | + result = (*bufferQueueItf)->RegisterCallback(bufferQueueItf, |
| 141 | + callback, |
| 142 | + this); |
| 143 | + if (SL_RESULT_SUCCESS != result) { |
| 144 | + LOGE("Player RegisterCallback failed!"); |
| 145 | + return 0; |
| 146 | + } |
| 147 | + result = (*playItf)->SetPlayState(playItf, SL_PLAYSTATE_PLAYING); |
| 148 | + if (SL_RESULT_SUCCESS != result) { |
| 149 | + LOGE("Player SetPlayState start failed!"); |
| 150 | + return 0; |
| 151 | + } |
| 152 | + callback(bufferQueueItf, this); |
| 153 | + return 1; |
| 154 | +} |
| 155 | + |
| 156 | +int AudioPlayer::write(uint8_t *buffer, size_t size) { |
| 157 | + this->size = size; |
| 158 | + this->buffer = buffer; |
| 159 | + this->requestRead = true; |
| 160 | +// (*bufferQueueItf)->Clear(bufferQueueItf); |
| 161 | + return 1; |
| 162 | +} |
| 163 | + |
| 164 | +void AudioPlayer::stop() { |
| 165 | + destroyEngine(); |
| 166 | + if (nullptr != quietBuffer) { |
| 167 | + free(quietBuffer); |
| 168 | + quietBuffer = nullptr; |
| 169 | + quietSize = 0; |
| 170 | + } |
| 171 | + callback = nullptr; |
| 172 | +} |
| 173 | + |
| 174 | +void AudioPlayer::destroyEngine() { |
| 175 | + if (nullptr != playObject) { |
| 176 | + (*playObject)->Destroy(playObject); |
| 177 | + playObject = nullptr; |
| 178 | + bufferQueueItf = nullptr; |
| 179 | + playItf = nullptr; |
| 180 | + } |
| 181 | + if (nullptr != mixObject) { |
| 182 | + (*mixObject)->Destroy(mixObject); |
| 183 | + mixObject = nullptr; |
| 184 | + } |
| 185 | + if (nullptr != engineObject) { |
| 186 | + (*engineObject)->Destroy(engineObject); |
| 187 | + engineObject = nullptr; |
| 188 | + engineItf = nullptr; |
| 189 | + } |
| 190 | +} |
0 commit comments