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