diff options
Diffstat (limited to 'plugins/audio_opensles/audio_opensles_device.cxx')
-rwxr-xr-x | plugins/audio_opensles/audio_opensles_device.cxx | 938 |
1 files changed, 480 insertions, 458 deletions
diff --git a/plugins/audio_opensles/audio_opensles_device.cxx b/plugins/audio_opensles/audio_opensles_device.cxx index 3c06bfc..e6fff79 100755 --- a/plugins/audio_opensles/audio_opensles_device.cxx +++ b/plugins/audio_opensles/audio_opensles_device.cxx @@ -23,64 +23,77 @@ #define CHECK_MICROPHONE_NOT_INITIALIZED() CHECK_FALSE(m_bMicrophoneInitialized, "Microphone initialized") #if AUDIO_OPENSLES_UNDER_ANDROID -static inline SLuint32 SL_SAMPLING_RATE(int RATE_INT){ - switch(RATE_INT){ - case 8000: return SL_SAMPLINGRATE_8; - case 11025: return SL_SAMPLINGRATE_11_025; - default:case 16000: return SL_SAMPLINGRATE_16; - case 22050: return SL_SAMPLINGRATE_22_05; - case 24000: return SL_SAMPLINGRATE_24; - case 32000: return SL_SAMPLINGRATE_32; - case 44100: return SL_SAMPLINGRATE_44_1; - case 64000: return SL_SAMPLINGRATE_64; - case 88200: return SL_SAMPLINGRATE_88_2; - case 96000: return SL_SAMPLINGRATE_96; - case 192000: return SL_SAMPLINGRATE_192; - } +static inline SLuint32 SL_SAMPLING_RATE(int RATE_INT) +{ + switch(RATE_INT) { + case 8000: + return SL_SAMPLINGRATE_8; + case 11025: + return SL_SAMPLINGRATE_11_025; + default: + case 16000: + return SL_SAMPLINGRATE_16; + case 22050: + return SL_SAMPLINGRATE_22_05; + case 24000: + return SL_SAMPLINGRATE_24; + case 32000: + return SL_SAMPLINGRATE_32; + case 44100: + return SL_SAMPLINGRATE_44_1; + case 64000: + return SL_SAMPLINGRATE_64; + case 88200: + return SL_SAMPLINGRATE_88_2; + case 96000: + return SL_SAMPLINGRATE_96; + case 192000: + return SL_SAMPLINGRATE_192; + } } #endif SLAudioDevice::SLAudioDevice(const SLAudioDeviceCallback* pCallback): #if AUDIO_OPENSLES_UNDER_ANDROID -m_slEngineObject(NULL), -m_slPlayer(NULL), -m_slEngine(NULL), -m_slPlayerPlay(NULL), -m_slPlayerSimpleBufferQueue(NULL), -m_slOutputMixObject(NULL), -m_slSpeakerVolume(NULL), -m_slRecorder(NULL), -m_slRecorderRecord(NULL), -m_slAudioIODeviceCapabilities(NULL), -m_slRecorderSimpleBufferQueue(NULL), -m_slMicVolume(NULL), -_playQueueSeq(0), -_recCurrentSeq(0), -_recBufferTotalSize(0), -_recQueueSeq(0), + m_slEngineObject(NULL), + m_slPlayer(NULL), + m_slEngine(NULL), + m_slPlayerPlay(NULL), + m_slPlayerSimpleBufferQueue(NULL), + m_slOutputMixObject(NULL), + m_slSpeakerVolume(NULL), + m_slRecorder(NULL), + m_slRecorderRecord(NULL), + m_slAudioIODeviceCapabilities(NULL), + m_slRecorderSimpleBufferQueue(NULL), + m_slMicVolume(NULL), + _playQueueSeq(0), + _recCurrentSeq(0), + _recBufferTotalSize(0), + _recQueueSeq(0), #endif -m_nMicDeviceId(0), -m_pCallback(pCallback), -m_bInitialized(false), -m_bSpeakerInitialized(false), -m_bSpeakerOn(false), -m_bPlayoutInitialized(false), -m_bRecordingInitialized(false), -m_bMicrophoneInitialized(false), -m_bStereoPlayout(false), -m_bStereoRecording(false), -m_nPlayoutSampleRate(PLAYOUT_SAMPLE_RATE), -m_nRecordingSampleRate(RECORDING_SAMPLE_RATE), -m_nRecordingBufferSize(RECORDING_BUFFER_SIZE), -m_nPlayoutBufferSize(PLAYOUT_BUFFER_SIZE), -m_bPlaying(false), -m_bRecording(false), -m_nSpeakerVolume(0), -m_nMinSpeakerVolume(0), -m_nMaxSpeakerVolume(0) + m_nMicDeviceId(0), + m_pCallback(pCallback), + m_bInitialized(false), + m_bSpeakerInitialized(false), + m_bSpeakerOn(false), + m_bPlayoutInitialized(false), + m_bRecordingInitialized(false), + m_bMicrophoneInitialized(false), + m_bStereoPlayout(false), + m_bStereoRecording(false), + m_nPlayoutSampleRate(PLAYOUT_SAMPLE_RATE), + m_nRecordingSampleRate(RECORDING_SAMPLE_RATE), + m_nRecordingBufferSize(RECORDING_BUFFER_SIZE), + m_nPlayoutBufferSize(PLAYOUT_BUFFER_SIZE), + m_bPlaying(false), + m_bRecording(false), + m_nSpeakerVolume(0), + m_nMinSpeakerVolume(0), + m_nMaxSpeakerVolume(0) { #if AUDIO_OPENSLES_UNDER_ANDROID - memset(_playQueueBuffer, 0, sizeof(_playQueueBuffer)); + memset(_playQueueBuffer, 0, sizeof(_playQueueBuffer)); memset(_recQueueBuffer, 0, sizeof(_recQueueBuffer)); memset(_recBuffer, 0, sizeof(_recBuffer)); memset(_recLength, 0, sizeof(_recLength)); @@ -94,22 +107,22 @@ SLAudioDevice::~SLAudioDevice() } int SLAudioDevice::SetCallback(const SLAudioDeviceCallback* pCallback) -{ - m_pCallback = pCallback; - return 0; +{ + m_pCallback = pCallback; + return 0; } int SLAudioDevice::Init() { - CHECK_FALSE(m_bInitialized, "Already initialized"); + CHECK_FALSE(m_bInitialized, "Already initialized"); - AUDIO_OPENSLES_DEBUG_INFO("SLAudioDevice::Init()"); + AUDIO_OPENSLES_DEBUG_INFO("SLAudioDevice::Init()"); #if AUDIO_OPENSLES_UNDER_ANDROID - SLresult slResult; + SLresult slResult; SLEngineOption EngineOption[] = { - { (SLuint32) SL_ENGINEOPTION_THREADSAFE, (SLuint32) SL_BOOLEAN_TRUE }, + { (SLuint32) SL_ENGINEOPTION_THREADSAFE, (SLuint32) SL_BOOLEAN_TRUE }, }; slResult = slCreateEngine(&m_slEngineObject, 1, EngineOption, 0, NULL, NULL); if (slResult != SL_RESULT_SUCCESS) { @@ -126,152 +139,160 @@ int SLAudioDevice::Init() } #endif /* AUDIO_OPENSLES_UNDER_ANDROID */ - m_bInitialized = true; - AUDIO_OPENSLES_DEBUG_INFO("SL engine initialized"); - return 0; + m_bInitialized = true; + AUDIO_OPENSLES_DEBUG_INFO("SL engine initialized"); + return 0; } bool SLAudioDevice::Initialized() { - return m_bInitialized; + return m_bInitialized; } int SLAudioDevice::SpeakerIsAvailable(bool *pAvailable) { - CHECK_TRUE(m_bInitialized, "Not initialized"); + CHECK_TRUE(m_bInitialized, "Not initialized"); - if(!pAvailable){ - AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter"); - return -1; - } + if(!pAvailable) { + AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter"); + return -1; + } - *pAvailable = true; - return 0; + *pAvailable = true; + return 0; } int SLAudioDevice::InitSpeaker() { - AUDIO_OPENSLES_DEBUG_INFO("SLAudioDevice::InitSpeaker()"); + AUDIO_OPENSLES_DEBUG_INFO("SLAudioDevice::InitSpeaker()"); - CHECK_TRUE(m_bInitialized, "Not initialized"); - - if(m_bSpeakerInitialized){ - return 0; - } + CHECK_TRUE(m_bInitialized, "Not initialized"); - m_bSpeakerInitialized = true; - return 0; + if(m_bSpeakerInitialized) { + return 0; + } + + m_bSpeakerInitialized = true; + return 0; } int SLAudioDevice::SetMaxSpeakerVolume(int nMaxSpeakerVolume) { - CHECK_TRUE(m_bSpeakerInitialized, "Speaker not initialized"); - AUDIO_OPENSLES_DEBUG_INFO("SetMaxSpeakerVolume(%d)", nMaxSpeakerVolume); - m_nMaxSpeakerVolume = nMaxSpeakerVolume; - return 0; + CHECK_TRUE(m_bSpeakerInitialized, "Speaker not initialized"); + AUDIO_OPENSLES_DEBUG_INFO("SetMaxSpeakerVolume(%d)", nMaxSpeakerVolume); + m_nMaxSpeakerVolume = nMaxSpeakerVolume; + return 0; } int SLAudioDevice::SetMinSpeakerVolume(int nMinSpeakerVolume) { - CHECK_TRUE(m_bSpeakerInitialized, "Speaker not initialized"); - AUDIO_OPENSLES_DEBUG_INFO("SetMinSpeakerVolume(%d)", nMinSpeakerVolume); - m_nMinSpeakerVolume = nMinSpeakerVolume; - return 0; + CHECK_TRUE(m_bSpeakerInitialized, "Speaker not initialized"); + AUDIO_OPENSLES_DEBUG_INFO("SetMinSpeakerVolume(%d)", nMinSpeakerVolume); + m_nMinSpeakerVolume = nMinSpeakerVolume; + return 0; } int SLAudioDevice::SetSpeakerVolume(int nSpeakerVolume) { - CHECK_TRUE(m_bSpeakerInitialized, "Speaker not initialized"); - AUDIO_OPENSLES_DEBUG_INFO("SetSpeakerVolume(%d)", nSpeakerVolume); - m_nSpeakerVolume = nSpeakerVolume; - return 0; + CHECK_TRUE(m_bSpeakerInitialized, "Speaker not initialized"); + AUDIO_OPENSLES_DEBUG_INFO("SetSpeakerVolume(%d)", nSpeakerVolume); + m_nSpeakerVolume = nSpeakerVolume; + return 0; } int SLAudioDevice::SetSpeakerOn(bool bSpeakerOn) { - CHECK_TRUE(m_bSpeakerInitialized, "Speaker not initialized"); - - AUDIO_OPENSLES_DEBUG_INFO("SetSpeakerOn(%s -> %s)", (m_bSpeakerOn ? "true" : "false"), (bSpeakerOn ? "true" : "false")); - int ret = 0; - bool oldValue = m_bSpeakerOn; - m_bSpeakerOn = bSpeakerOn; // update value beacause use in PlayoutApplyNewConfig(); - if(m_bPlayoutInitialized && (oldValue != bSpeakerOn)){ - ret = PlayoutApplyNewConfig(); - } - - if(ret != 0){ - m_bSpeakerOn = oldValue; - } - - return ret; + CHECK_TRUE(m_bSpeakerInitialized, "Speaker not initialized"); + + AUDIO_OPENSLES_DEBUG_INFO("SetSpeakerOn(%s -> %s)", (m_bSpeakerOn ? "true" : "false"), (bSpeakerOn ? "true" : "false")); + int ret = 0; + bool oldValue = m_bSpeakerOn; + m_bSpeakerOn = bSpeakerOn; // update value beacause use in PlayoutApplyNewConfig(); + if(m_bPlayoutInitialized && (oldValue != bSpeakerOn)) { + ret = PlayoutApplyNewConfig(); + } + + if(ret != 0) { + m_bSpeakerOn = oldValue; + } + + return ret; } int SLAudioDevice::PlayoutIsAvailable(bool *pAvailable) { - CHECK_TRUE(m_bInitialized, "Not initialized"); - CHECK_PLAYOUT_NOT_INITIALIZED(); + CHECK_TRUE(m_bInitialized, "Not initialized"); + CHECK_PLAYOUT_NOT_INITIALIZED(); - if(!pAvailable){ - AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter"); - return -1; - } + if(!pAvailable) { + AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter"); + return -1; + } - *pAvailable = true; - return 0; + *pAvailable = true; + return 0; } int SLAudioDevice::SetStereoPlayout(bool bEnabled) { - CHECK_TRUE(m_bInitialized, "Not initialized"); - CHECK_PLAYOUT_NOT_INITIALIZED(); - m_bStereoPlayout = bEnabled; - return 0; + CHECK_TRUE(m_bInitialized, "Not initialized"); + CHECK_PLAYOUT_NOT_INITIALIZED(); + m_bStereoPlayout = bEnabled; + return 0; } int SLAudioDevice::SetPlayoutBuffer(int nPlayoutBufferSize) { - CHECK_TRUE(m_bInitialized, "Not initialized"); - CHECK_PLAYOUT_NOT_INITIALIZED(); + CHECK_TRUE(m_bInitialized, "Not initialized"); + CHECK_PLAYOUT_NOT_INITIALIZED(); - if(PLAYOUT_BUFFER_SIZE != nPlayoutBufferSize){ - AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter"); - return -1; - } + if(PLAYOUT_BUFFER_SIZE != nPlayoutBufferSize) { + AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter"); + return -1; + } - m_nPlayoutBufferSize = nPlayoutBufferSize; - return 0; + m_nPlayoutBufferSize = nPlayoutBufferSize; + return 0; } int SLAudioDevice::SetPlayoutSampleRate(int nPlayoutSampleRate) { - CHECK_TRUE(m_bInitialized, "Not initialized"); - CHECK_PLAYOUT_NOT_INITIALIZED(); - - AUDIO_OPENSLES_DEBUG_INFO("SetPlayoutSampleRate(%d)", nPlayoutSampleRate); - - switch(nPlayoutSampleRate){ - case 8000: case 11025: case 16000: case 22050: case 24000: case 32000: case 44100: case 64000: case 88200: case 96000: case 192000: - { - m_nPlayoutSampleRate = nPlayoutSampleRate; - return 0; - } - default: - { - AUDIO_OPENSLES_DEBUG_ERROR("%d not valid sampling rate", nPlayoutSampleRate); - return -1; - } - } + CHECK_TRUE(m_bInitialized, "Not initialized"); + CHECK_PLAYOUT_NOT_INITIALIZED(); + + AUDIO_OPENSLES_DEBUG_INFO("SetPlayoutSampleRate(%d)", nPlayoutSampleRate); + + switch(nPlayoutSampleRate) { + case 8000: + case 11025: + case 16000: + case 22050: + case 24000: + case 32000: + case 44100: + case 64000: + case 88200: + case 96000: + case 192000: { + m_nPlayoutSampleRate = nPlayoutSampleRate; + return 0; + } + default: { + AUDIO_OPENSLES_DEBUG_ERROR("%d not valid sampling rate", nPlayoutSampleRate); + return -1; + } + } } int SLAudioDevice::InitPlayout() { - CHECK_TRUE(m_bInitialized, "Not initialized"); - - AUDIO_OPENSLES_DEBUG_INFO("SLAudioDevice::InitPlayout()"); + CHECK_TRUE(m_bInitialized, "Not initialized"); - if(m_bPlayoutInitialized){ - return 0; - } + AUDIO_OPENSLES_DEBUG_INFO("SLAudioDevice::InitPlayout()"); + + if(m_bPlayoutInitialized) { + return 0; + } if (m_bPlaying) { AUDIO_OPENSLES_DEBUG_ERROR("Playout already started"); @@ -285,7 +306,7 @@ int SLAudioDevice::InitPlayout() #if AUDIO_OPENSLES_UNDER_ANDROID - if (m_slEngineObject == NULL || m_slEngine == NULL) { + if (m_slEngineObject == NULL || m_slEngine == NULL) { AUDIO_OPENSLES_DEBUG_ERROR("SLObject or Engiine is NULL"); return -1; } @@ -305,68 +326,68 @@ int SLAudioDevice::InitPlayout() req[i] = SL_BOOLEAN_FALSE; } ids[0] = SL_IID_ENVIRONMENTALREVERB; - + if ((slResult = (*m_slEngine)->CreateOutputMix(m_slEngine, &m_slOutputMixObject, 1, ids, req)) != SL_RESULT_SUCCESS) { AUDIO_OPENSLES_DEBUG_ERROR("CreateOutputMix() for playout failed with error code = %d", slResult); return -1; } - + if ((slResult = (*m_slOutputMixObject)->Realize(m_slOutputMixObject, SL_BOOLEAN_FALSE)) != SL_RESULT_SUCCESS) { AUDIO_OPENSLES_DEBUG_ERROR("Failed to realize SL Output Mix object for playout with error code = %d", slResult); return -1; } - + simpleBufferQueue.locatorType = SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE; simpleBufferQueue.numBuffers = N_PLAY_QUEUE_BUFFERS; - + pcm.formatType = SL_DATAFORMAT_PCM; - pcm.numChannels = m_bStereoPlayout ? 2 : 1; - pcm.samplesPerSec = SL_SAMPLING_RATE(m_nPlayoutSampleRate); + pcm.numChannels = m_bStereoPlayout ? 2 : 1; + pcm.samplesPerSec = SL_SAMPLING_RATE(m_nPlayoutSampleRate); pcm.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16; pcm.containerSize = SL_PCMSAMPLEFORMAT_FIXED_16; - pcm.channelMask = m_bStereoRecording ? (SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT) : SL_SPEAKER_FRONT_CENTER; + pcm.channelMask = m_bStereoRecording ? (SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT) : SL_SPEAKER_FRONT_CENTER; pcm.endianness = SL_BYTEORDER_LITTLEENDIAN; - - audioSource.pFormat = (void *) &pcm; + + audioSource.pFormat = (void *) &pcm; audioSource.pLocator = (void *) &simpleBufferQueue; locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX; locator_outputmix.outputMix = m_slOutputMixObject; audioSink.pLocator = (void *) &locator_outputmix; audioSink.pFormat = NULL; - + ids[0] = SL_IID_ANDROIDSIMPLEBUFFERQUEUE; ids[1] = SL_IID_EFFECTSEND; - ids[2] = SL_IID_ANDROIDCONFIGURATION; - ids[3] = SL_IID_VOLUME; + ids[2] = SL_IID_ANDROIDCONFIGURATION; + ids[3] = SL_IID_VOLUME; req[0] = SL_BOOLEAN_TRUE; req[1] = SL_BOOLEAN_TRUE; - req[2] = SL_BOOLEAN_TRUE; - req[3] = SL_BOOLEAN_TRUE; + req[2] = SL_BOOLEAN_TRUE; + req[3] = SL_BOOLEAN_TRUE; // Create the player if ((slResult = (*m_slEngine)->CreateAudioPlayer(m_slEngine, &m_slPlayer, &audioSource, &audioSink, 3, ids, req)) != SL_RESULT_SUCCESS) { AUDIO_OPENSLES_DEBUG_ERROR("Failed to create Audio Player with error code = %d", slResult); return -1; } - - // set stream type - if(!m_bSpeakerOn){ // only set if speaker OFF, otherwise default is ON. "SL_ANDROID_STREAM_MEDIA" doen't look to work on all devices - static SLAndroidConfigurationItf _playerStreamConfig; - if((slResult = (*m_slPlayer)->GetInterface(m_slPlayer, SL_IID_ANDROIDCONFIGURATION, &_playerStreamConfig)) != SL_RESULT_SUCCESS){ - AUDIO_OPENSLES_DEBUG_ERROR("Failed to get player configuration with error code = %d", slResult); - return -1; - } - else{ - static SLint32 _playerStreamType = m_bSpeakerOn ? SL_ANDROID_STREAM_MEDIA : SL_ANDROID_STREAM_VOICE; - static SLint32 _playerStreamTypeSize = sizeof(SLint32); - AUDIO_OPENSLES_DEBUG_INFO("_playerStreamType=%d", _playerStreamType); - if((slResult = (*_playerStreamConfig)->SetConfiguration(_playerStreamConfig, SL_ANDROID_KEY_STREAM_TYPE, &_playerStreamType, _playerStreamTypeSize))){ - AUDIO_OPENSLES_DEBUG_ERROR("Failed to set player stream type with error code = %d", slResult); - return -2; - } - } - } + + // set stream type + if(!m_bSpeakerOn) { // only set if speaker OFF, otherwise default is ON. "SL_ANDROID_STREAM_MEDIA" doen't look to work on all devices + static SLAndroidConfigurationItf _playerStreamConfig; + if((slResult = (*m_slPlayer)->GetInterface(m_slPlayer, SL_IID_ANDROIDCONFIGURATION, &_playerStreamConfig)) != SL_RESULT_SUCCESS) { + AUDIO_OPENSLES_DEBUG_ERROR("Failed to get player configuration with error code = %d", slResult); + return -1; + } + else { + static SLint32 _playerStreamType = m_bSpeakerOn ? SL_ANDROID_STREAM_MEDIA : SL_ANDROID_STREAM_VOICE; + static SLint32 _playerStreamTypeSize = sizeof(SLint32); + AUDIO_OPENSLES_DEBUG_INFO("_playerStreamType=%d", _playerStreamType); + if((slResult = (*_playerStreamConfig)->SetConfiguration(_playerStreamConfig, SL_ANDROID_KEY_STREAM_TYPE, &_playerStreamType, _playerStreamTypeSize))) { + AUDIO_OPENSLES_DEBUG_ERROR("Failed to set player stream type with error code = %d", slResult); + return -2; + } + } + } // Realizing the player in synchronous mode if ((slResult = (*m_slPlayer)->Realize(m_slPlayer, SL_BOOLEAN_FALSE)) != SL_RESULT_SUCCESS) { @@ -384,53 +405,53 @@ int SLAudioDevice::InitPlayout() } // Setup to receive buffer queue event callbacks - if ((slResult = (*m_slPlayerSimpleBufferQueue)->RegisterCallback(m_slPlayerSimpleBufferQueue, PlayerSimpleBufferQueueCallback, this)) != SL_RESULT_SUCCESS) { + if ((slResult = (*m_slPlayerSimpleBufferQueue)->RegisterCallback(m_slPlayerSimpleBufferQueue, PlayerSimpleBufferQueueCallback, this)) != SL_RESULT_SUCCESS) { AUDIO_OPENSLES_DEBUG_ERROR("Failed to register Player Callback"); return -1; } #endif /* AUDIO_OPENSLES_UNDER_ANDROID */ - m_bPlayoutInitialized = true; + m_bPlayoutInitialized = true; - AUDIO_OPENSLES_DEBUG_INFO("Playout initialized"); + AUDIO_OPENSLES_DEBUG_INFO("Playout initialized"); - return 0; + return 0; } int SLAudioDevice::StereoPlayout(bool *pEnabled) { - CHECK_TRUE(m_bInitialized, "Not initialized"); - CHECK_PLAYOUT_INITIALIZED(); + CHECK_TRUE(m_bInitialized, "Not initialized"); + CHECK_PLAYOUT_INITIALIZED(); - if(!pEnabled){ - AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter"); - return -1; - } + if(!pEnabled) { + AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter"); + return -1; + } - *pEnabled = m_bStereoPlayout; - return 0; + *pEnabled = m_bStereoPlayout; + return 0; } int SLAudioDevice::PlayoutSampleRate(int *pPlayoutSampleRate) { - CHECK_TRUE(m_bInitialized, "Not initialized"); - CHECK_PLAYOUT_INITIALIZED(); + CHECK_TRUE(m_bInitialized, "Not initialized"); + CHECK_PLAYOUT_INITIALIZED(); - if(!pPlayoutSampleRate){ - AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter"); - return -1; - } + if(!pPlayoutSampleRate) { + AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter"); + return -1; + } - *pPlayoutSampleRate = m_nPlayoutSampleRate; - return 0; + *pPlayoutSampleRate = m_nPlayoutSampleRate; + return 0; } int SLAudioDevice::StartPlayout() { - AUDIO_OPENSLES_DEBUG_INFO("SLAudioDevice::StartPlayout()"); + AUDIO_OPENSLES_DEBUG_INFO("SLAudioDevice::StartPlayout()"); - CHECK_TRUE(m_bInitialized, "Not initialized"); - CHECK_PLAYOUT_INITIALIZED(); + CHECK_TRUE(m_bInitialized, "Not initialized"); + CHECK_PLAYOUT_INITIALIZED(); if (m_bPlaying) { return 0; @@ -450,21 +471,21 @@ int SLAudioDevice::StartPlayout() SLresult slResult; /* Enqueue a set of zero buffers to get the ball rolling */ - uint32_t nSample10ms = m_nPlayoutSampleRate / 100; + uint32_t nSample10ms = m_nPlayoutSampleRate / 100; uint8_t playBuffer[nSample10ms << BYTES_PER_SAMPLE_LOG2]; uint32_t noSamplesOut(0); { - // get data from jitter buffer - noSamplesOut = SLAudioDevice::PullPlayoutData(playBuffer, nSample10ms); - if(noSamplesOut != nSample10ms){ - AUDIO_OPENSLES_DEBUG_WARN("%d not expected as samples output count value", noSamplesOut); - noSamplesOut = nSample10ms; - memset(_playQueueBuffer[_playQueueSeq], 0, (noSamplesOut << BYTES_PER_SAMPLE_LOG2)); - } - else{ - memcpy(_playQueueBuffer[_playQueueSeq], playBuffer, (noSamplesOut << BYTES_PER_SAMPLE_LOG2)); - } - + // get data from jitter buffer + noSamplesOut = SLAudioDevice::PullPlayoutData(playBuffer, nSample10ms); + if(noSamplesOut != nSample10ms) { + AUDIO_OPENSLES_DEBUG_WARN("%d not expected as samples output count value", noSamplesOut); + noSamplesOut = nSample10ms; + memset(_playQueueBuffer[_playQueueSeq], 0, (noSamplesOut << BYTES_PER_SAMPLE_LOG2)); + } + else { + memcpy(_playQueueBuffer[_playQueueSeq], playBuffer, (noSamplesOut << BYTES_PER_SAMPLE_LOG2)); + } + // write the buffer data we into the device if ((slResult = (*m_slPlayerSimpleBufferQueue)->Enqueue(m_slPlayerSimpleBufferQueue, (void*) _playQueueBuffer[_playQueueSeq], (noSamplesOut << 1))) != SL_RESULT_SUCCESS) { AUDIO_OPENSLES_DEBUG_ERROR("Player simpler buffer queue Enqueue failed with error code = %d and noSamplesOut = %d", slResult, noSamplesOut); @@ -473,42 +494,42 @@ int SLAudioDevice::StartPlayout() } // Play the PCM samples using a buffer queue - m_bPlaying = true; + m_bPlaying = true; if ((slResult = (*m_slPlayerPlay)->SetPlayState(m_slPlayerPlay, SL_PLAYSTATE_PLAYING)) != SL_RESULT_SUCCESS) { AUDIO_OPENSLES_DEBUG_ERROR("Failed to start playout with error code = %d", slResult); - m_bPlaying = false; + m_bPlaying = false; return -1; } #else - m_bPlaying = true; + m_bPlaying = true; #endif /* AUDIO_OPENSLES_UNDER_ANDROID */ - AUDIO_OPENSLES_DEBUG_INFO("Payout started - rate=%d", m_nPlayoutSampleRate); + AUDIO_OPENSLES_DEBUG_INFO("Payout started - rate=%d", m_nPlayoutSampleRate); - return 0; + return 0; } bool SLAudioDevice::Playing() { - return m_bPlaying; + return m_bPlaying; } int SLAudioDevice::StopPlayout() { - AUDIO_OPENSLES_DEBUG_INFO("SLAudioDevice::StopPlayout()"); + AUDIO_OPENSLES_DEBUG_INFO("SLAudioDevice::StopPlayout()"); - if(!m_bPlaying){ - return 0; - } + if(!m_bPlaying) { + return 0; + } #if AUDIO_OPENSLES_UNDER_ANDROID if ((m_slPlayerPlay != NULL) && (m_slOutputMixObject != NULL) && (m_slPlayer != NULL)) { - SLresult slResult; - + SLresult slResult; + if ((slResult = (*m_slPlayerPlay)->SetPlayState(m_slPlayerPlay, SL_PLAYSTATE_STOPPED)) != SL_RESULT_SUCCESS) { AUDIO_OPENSLES_DEBUG_ERROR("Failed to stop playout with error code = %d", slResult); return -1; } - + if ((slResult = (*m_slPlayerSimpleBufferQueue)->Clear(m_slPlayerSimpleBufferQueue)) != SL_RESULT_SUCCESS) { AUDIO_OPENSLES_DEBUG_ERROR("Failed to clear recorder buffer queue"); return -1; @@ -516,7 +537,7 @@ int SLAudioDevice::StopPlayout() // Destroy the player (*m_slPlayer)->Destroy(m_slPlayer); - // Destroy Output Mix object + // Destroy Output Mix object (*m_slOutputMixObject)->Destroy(m_slOutputMixObject); m_slPlayer = NULL; m_slPlayerPlay = NULL; @@ -525,124 +546,132 @@ int SLAudioDevice::StopPlayout() } #endif - AUDIO_OPENSLES_DEBUG_INFO("Playout stopped"); - m_bPlayoutInitialized = false; - m_bPlaying = false; - return 0; + AUDIO_OPENSLES_DEBUG_INFO("Playout stopped"); + m_bPlayoutInitialized = false; + m_bPlaying = false; + return 0; } int SLAudioDevice::RecordingIsAvailable(bool *pAvailable) { - CHECK_TRUE(m_bInitialized, "Device not initialized"); - CHECK_RECORDING_NOT_INITIALIZED(); + CHECK_TRUE(m_bInitialized, "Device not initialized"); + CHECK_RECORDING_NOT_INITIALIZED(); - if(!pAvailable){ - AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter"); - return -1; - } + if(!pAvailable) { + AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter"); + return -1; + } - *pAvailable = true; - return 0; + *pAvailable = true; + return 0; } int SLAudioDevice::MicrophoneIsAvailable(bool *pAvailable) { - CHECK_TRUE(m_bInitialized, "Device not initialized"); - CHECK_RECORDING_NOT_INITIALIZED(); + CHECK_TRUE(m_bInitialized, "Device not initialized"); + CHECK_RECORDING_NOT_INITIALIZED(); - if(!pAvailable){ - AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter"); - return -1; - } + if(!pAvailable) { + AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter"); + return -1; + } - *pAvailable = true; - return 0; + *pAvailable = true; + return 0; } int SLAudioDevice::InitMicrophone() { - AUDIO_OPENSLES_DEBUG_INFO("SLAudioDevice::InitMicrophone()"); - CHECK_TRUE(m_bInitialized, "Device not initialized"); + AUDIO_OPENSLES_DEBUG_INFO("SLAudioDevice::InitMicrophone()"); + CHECK_TRUE(m_bInitialized, "Device not initialized"); - if(m_bMicrophoneInitialized){ - return 0; - } + if(m_bMicrophoneInitialized) { + return 0; + } - m_bMicrophoneInitialized = true; - return 0; + m_bMicrophoneInitialized = true; + return 0; } int SLAudioDevice::SetMicrophoneVolume(int nMicrophoneVolume) { - CHECK_MICROPHONE_INITIALIZED(); + CHECK_MICROPHONE_INITIALIZED(); - AUDIO_OPENSLES_DEBUG_INFO("SetMicrophoneVolume(%d)", nMicrophoneVolume); + AUDIO_OPENSLES_DEBUG_INFO("SetMicrophoneVolume(%d)", nMicrophoneVolume); #if AUDIO_OPENSLES_UNDER_ANDROID if (m_slMicVolume == NULL) { - SLresult slResult; + SLresult slResult; if ((slResult = (*m_slEngineObject)->GetInterface(m_slEngineObject, SL_IID_DEVICEVOLUME, (void*) &m_slMicVolume)) != SL_RESULT_SUCCESS) { AUDIO_OPENSLES_DEBUG_ERROR("Failed to get 'SL_IID_DEVICEVOLUME' interface with error code = %d", slResult); - return -1; + return -1; } } if (m_slMicVolume != NULL) { - SLresult slResult; + SLresult slResult; int vol(0); vol = ((nMicrophoneVolume * (m_nMaxSpeakerVolume - m_nMinSpeakerVolume) + (int) (255 / 2)) / (255)) + m_nMinSpeakerVolume; if ((slResult = (*m_slMicVolume)->SetVolume(m_slMicVolume, m_nMicDeviceId, vol)) != SL_RESULT_SUCCESS) { AUDIO_OPENSLES_DEBUG_ERROR("SetVolume() failed with error code = %d", slResult); - return -1; + return -1; } } #endif /* AUDIO_OPENSLES_UNDER_ANDROID */ - return 0; + return 0; } int SLAudioDevice::SetStereoRecording(bool bEnabled) { - CHECK_TRUE(m_bInitialized, "Not initialized"); - CHECK_RECORDING_NOT_INITIALIZED(); - AUDIO_OPENSLES_DEBUG_INFO("SetStereoRecording(%s)", bEnabled ? "True" : "False"); - m_bStereoRecording = bEnabled; - return 0; + CHECK_TRUE(m_bInitialized, "Not initialized"); + CHECK_RECORDING_NOT_INITIALIZED(); + AUDIO_OPENSLES_DEBUG_INFO("SetStereoRecording(%s)", bEnabled ? "True" : "False"); + m_bStereoRecording = bEnabled; + return 0; } int SLAudioDevice::SetRecordingSampleRate(int nRecordingSampleRate) { - CHECK_TRUE(m_bInitialized, "Not initialized"); - CHECK_RECORDING_NOT_INITIALIZED(); - - AUDIO_OPENSLES_DEBUG_INFO("SetRecordingSampleRate(%d)", nRecordingSampleRate); - - switch(nRecordingSampleRate){ - case 8000: case 11025: case 16000: case 22050: case 24000: case 32000: case 44100: case 64000: case 88200: case 96000: case 192000: - { - m_nRecordingSampleRate = nRecordingSampleRate; - return 0; - } - default: - { - AUDIO_OPENSLES_DEBUG_ERROR("%d not valid sampling rate", nRecordingSampleRate); - return -1; - } - } + CHECK_TRUE(m_bInitialized, "Not initialized"); + CHECK_RECORDING_NOT_INITIALIZED(); + + AUDIO_OPENSLES_DEBUG_INFO("SetRecordingSampleRate(%d)", nRecordingSampleRate); + + switch(nRecordingSampleRate) { + case 8000: + case 11025: + case 16000: + case 22050: + case 24000: + case 32000: + case 44100: + case 64000: + case 88200: + case 96000: + case 192000: { + m_nRecordingSampleRate = nRecordingSampleRate; + return 0; + } + default: { + AUDIO_OPENSLES_DEBUG_ERROR("%d not valid sampling rate", nRecordingSampleRate); + return -1; + } + } } int SLAudioDevice::InitRecording() { - CHECK_TRUE(m_bInitialized, "Not initialized"); + CHECK_TRUE(m_bInitialized, "Not initialized"); - AUDIO_OPENSLES_DEBUG_INFO("SLAudioDevice::InitRecording()"); + AUDIO_OPENSLES_DEBUG_INFO("SLAudioDevice::InitRecording()"); if (m_bRecording) { AUDIO_OPENSLES_DEBUG_ERROR("Recording already started"); return -1; } - if (m_bRecordingInitialized) { + if (m_bRecordingInitialized) { return 0; } @@ -696,21 +725,21 @@ int SLAudioDevice::InitRecording() return -1; } - // Set stream type - SLAndroidConfigurationItf slRecorderConfig; - SLint32 slStreamType = SL_ANDROID_RECORDING_PRESET_GENERIC; + // Set stream type + SLAndroidConfigurationItf slRecorderConfig; + SLint32 slStreamType = SL_ANDROID_RECORDING_PRESET_GENERIC; slResult = (*m_slRecorder)->GetInterface(m_slRecorder, SL_IID_ANDROIDCONFIGURATION, &slRecorderConfig); if(slResult != SL_RESULT_SUCCESS) { AUDIO_OPENSLES_DEBUG_ERROR("GetInterface(SL_IID_ANDROIDCONFIGURATION) failed with error code = %d", slResult); return -1; } - AUDIO_OPENSLES_DEBUG_INFO("Recording stream type = %d", slStreamType); + AUDIO_OPENSLES_DEBUG_INFO("Recording stream type = %d", slStreamType); slResult = (*slRecorderConfig)->SetConfiguration(slRecorderConfig, SL_ANDROID_KEY_RECORDING_PRESET, &slStreamType, sizeof(SLint32)); - if(slResult != SL_RESULT_SUCCESS) { + if(slResult != SL_RESULT_SUCCESS) { AUDIO_OPENSLES_DEBUG_ERROR("SetConfiguration(SL_ANDROID_KEY_RECORDING_PRESET) failed with error code = %d", slResult); return -1; } - + // Realizing the recorder in synchronous mode. slResult = (*m_slRecorder)->Realize(m_slRecorder, SL_BOOLEAN_FALSE); @@ -742,47 +771,47 @@ int SLAudioDevice::InitRecording() #endif /* AUDIO_OPENSLES_UNDER_ANDROID */ - AUDIO_OPENSLES_DEBUG_INFO("Recording initialized"); + AUDIO_OPENSLES_DEBUG_INFO("Recording initialized"); - m_bRecordingInitialized = true; + m_bRecordingInitialized = true; - return 0; + return 0; } int SLAudioDevice::StereoRecording(bool *pEnabled) { - CHECK_TRUE(m_bInitialized, "Not initialized"); - CHECK_RECORDING_INITIALIZED(); + CHECK_TRUE(m_bInitialized, "Not initialized"); + CHECK_RECORDING_INITIALIZED(); - if(!pEnabled){ - AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter"); - return -1; - } + if(!pEnabled) { + AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter"); + return -1; + } - *pEnabled = m_bStereoRecording; - return 0; + *pEnabled = m_bStereoRecording; + return 0; } int SLAudioDevice::RecordingSampleRate(int *pRecordingSampleRate) { - CHECK_TRUE(m_bInitialized, "Not initialized"); - CHECK_RECORDING_INITIALIZED(); + CHECK_TRUE(m_bInitialized, "Not initialized"); + CHECK_RECORDING_INITIALIZED(); - if(!pRecordingSampleRate){ - AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter"); - return -1; - } + if(!pRecordingSampleRate) { + AUDIO_OPENSLES_DEBUG_ERROR("Invalid parameter"); + return -1; + } - *pRecordingSampleRate = m_nRecordingSampleRate; - return 0; + *pRecordingSampleRate = m_nRecordingSampleRate; + return 0; } int SLAudioDevice::StartRecording() { - AUDIO_OPENSLES_DEBUG_INFO("SLAudioDevice::StartRecording()"); + AUDIO_OPENSLES_DEBUG_INFO("SLAudioDevice::StartRecording()"); - CHECK_TRUE(m_bInitialized, "Not initialized"); - CHECK_RECORDING_INITIALIZED(); + CHECK_TRUE(m_bInitialized, "Not initialized"); + CHECK_RECORDING_INITIALIZED(); if (m_bRecording) { return 0; @@ -791,13 +820,13 @@ int SLAudioDevice::StartRecording() #if AUDIO_OPENSLES_UNDER_ANDROID if (m_slRecorderRecord == NULL) { - AUDIO_OPENSLES_DEBUG_ERROR("RecordITF is NULL"); + AUDIO_OPENSLES_DEBUG_ERROR("RecordITF is NULL"); return -1; } if (m_slRecorderSimpleBufferQueue == NULL) { - AUDIO_OPENSLES_DEBUG_ERROR("Recorder Simple Buffer Queue is NULL"); - return -1; + AUDIO_OPENSLES_DEBUG_ERROR("Recorder Simple Buffer Queue is NULL"); + return -1; } // Reset recording buffer @@ -822,31 +851,31 @@ int SLAudioDevice::StartRecording() _recQueueSeq++; } // Record the audio - m_bRecording = true; + m_bRecording = true; slResult = (*m_slRecorderRecord)->SetRecordState(m_slRecorderRecord, SL_RECORDSTATE_RECORDING); if (slResult != SL_RESULT_SUCCESS) { AUDIO_OPENSLES_DEBUG_ERROR("Failed to start recording with error code = %d", slResult); - m_bRecording = false; + m_bRecording = false; return -1; } #else - m_bRecording = true; + m_bRecording = true; #endif /* AUDIO_OPENSLES_UNDER_ANDROID */ - AUDIO_OPENSLES_DEBUG_INFO("Recording started - rate = %d", m_nRecordingSampleRate); - - return 0; + AUDIO_OPENSLES_DEBUG_INFO("Recording started - rate = %d", m_nRecordingSampleRate); + + return 0; } bool SLAudioDevice::Recording() { - return m_bRecording; + return m_bRecording; } int SLAudioDevice::StopRecording() { - AUDIO_OPENSLES_DEBUG_INFO("SLAudioDevice::StopRecording()"); - if (!m_bRecording) { + AUDIO_OPENSLES_DEBUG_INFO("SLAudioDevice::StopRecording()"); + if (!m_bRecording) { return 0; } #if AUDIO_OPENSLES_UNDER_ANDROID @@ -870,47 +899,47 @@ int SLAudioDevice::StopRecording() } #endif - AUDIO_OPENSLES_DEBUG_INFO("Recording stopped"); - m_bRecording = false; - m_bRecordingInitialized = false; - return 0; + AUDIO_OPENSLES_DEBUG_INFO("Recording stopped"); + m_bRecording = false; + m_bRecordingInitialized = false; + return 0; } int SLAudioDevice::Terminate() { - if (!m_bInitialized) { + if (!m_bInitialized) { return 0; } - if(Recording()){ - StopRecording(); - } + if(Recording()) { + StopRecording(); + } - if(Playing()){ - StopPlayout(); - } + if(Playing()) { + StopPlayout(); + } #if AUDIO_OPENSLES_UNDER_ANDROID - if(m_slPlayer){ - (*m_slPlayer)->Destroy(m_slPlayer); - m_slPlayer = NULL; - m_slPlayerPlay = NULL; - m_slPlayerSimpleBufferQueue = NULL; - } - - if(m_slRecorder){ - (*m_slRecorder)->Destroy(m_slRecorder); - m_slRecorder = NULL; - m_slRecorderRecord = NULL; - m_slRecorderSimpleBufferQueue = NULL; - m_slAudioIODeviceCapabilities = NULL; - } - - if(m_slOutputMixObject){ - (*m_slOutputMixObject)->Destroy(m_slOutputMixObject); - m_slOutputMixObject = NULL; - } + if(m_slPlayer) { + (*m_slPlayer)->Destroy(m_slPlayer); + m_slPlayer = NULL; + m_slPlayerPlay = NULL; + m_slPlayerSimpleBufferQueue = NULL; + } + + if(m_slRecorder) { + (*m_slRecorder)->Destroy(m_slRecorder); + m_slRecorder = NULL; + m_slRecorderRecord = NULL; + m_slRecorderSimpleBufferQueue = NULL; + m_slAudioIODeviceCapabilities = NULL; + } + + if(m_slOutputMixObject) { + (*m_slOutputMixObject)->Destroy(m_slOutputMixObject); + m_slOutputMixObject = NULL; + } if (m_slEngineObject) { (*m_slEngineObject)->Destroy(m_slEngineObject); @@ -919,9 +948,9 @@ int SLAudioDevice::Terminate() } #endif - m_bSpeakerInitialized = false; - m_bPlayoutInitialized = false; - m_bRecordingInitialized = false; + m_bSpeakerInitialized = false; + m_bPlayoutInitialized = false; + m_bRecordingInitialized = false; m_bInitialized = false; return 0; @@ -929,73 +958,73 @@ int SLAudioDevice::Terminate() int SLAudioDevice::PlayoutApplyNewConfig() { - AUDIO_OPENSLES_DEBUG_INFO("SLAudioDevice::PlayoutApplyNewConfig()"); + AUDIO_OPENSLES_DEBUG_INFO("SLAudioDevice::PlayoutApplyNewConfig()"); #if AUDIO_OPENSLES_UNDER_ANDROID - if(m_slPlayer){ - SLresult slResult; - int ret; - bool wasPlaying = Playing(); - - if(wasPlaying){ - if ((ret = StopPlayout())){ - AUDIO_OPENSLES_DEBUG_ERROR("Failed to stop playout for reconf"); - return ret; - } - if((ret = InitPlayout())){ - AUDIO_OPENSLES_DEBUG_ERROR("Failed to init() playout after reconf"); - return ret; - } - } - - if(wasPlaying){ - if((ret = StartPlayout())){ - AUDIO_OPENSLES_DEBUG_ERROR("Failed to start() playout after reconf"); - return ret; - } - } - } + if(m_slPlayer) { + SLresult slResult; + int ret; + bool wasPlaying = Playing(); + + if(wasPlaying) { + if ((ret = StopPlayout())) { + AUDIO_OPENSLES_DEBUG_ERROR("Failed to stop playout for reconf"); + return ret; + } + if((ret = InitPlayout())) { + AUDIO_OPENSLES_DEBUG_ERROR("Failed to init() playout after reconf"); + return ret; + } + } + + if(wasPlaying) { + if((ret = StartPlayout())) { + AUDIO_OPENSLES_DEBUG_ERROR("Failed to start() playout after reconf"); + return ret; + } + } + } #endif /* AUDIO_OPENSLES_UNDER_ANDROID */ - return 0; + return 0; } uint32_t SLAudioDevice::PullPlayoutData(void* pAudioSamples, const uint32_t nSamples) { - if(!pAudioSamples || !nSamples){ - AUDIO_OPENSLES_DEBUG_ERROR("PullPlayoutData() - Invalid parameter"); - return 0; - } - - if(!m_pCallback){ - memset(pAudioSamples, 0, (nSamples << BYTES_PER_SAMPLE_LOG2)); - return nSamples; - } - - uint32_t nSamplesOut = 0; - const_cast<SLAudioDeviceCallback*>(m_pCallback)->NeedMorePlayData(nSamples, - BYTES_PER_SAMPLE, - m_bStereoPlayout ? 2 : 1, - m_nPlayoutSampleRate, - pAudioSamples, - nSamplesOut); - return nSamplesOut; + if(!pAudioSamples || !nSamples) { + AUDIO_OPENSLES_DEBUG_ERROR("PullPlayoutData() - Invalid parameter"); + return 0; + } + + if(!m_pCallback) { + memset(pAudioSamples, 0, (nSamples << BYTES_PER_SAMPLE_LOG2)); + return nSamples; + } + + uint32_t nSamplesOut = 0; + const_cast<SLAudioDeviceCallback*>(m_pCallback)->NeedMorePlayData(nSamples, + BYTES_PER_SAMPLE, + m_bStereoPlayout ? 2 : 1, + m_nPlayoutSampleRate, + pAudioSamples, + nSamplesOut); + return nSamplesOut; } void SLAudioDevice::PushRecordingData(void* pAudioSamples, const uint32_t nSamples) { - if(!pAudioSamples || !nSamples){ - AUDIO_OPENSLES_DEBUG_ERROR("PushRecordingData() - Invalid parameter"); - return; - } - - if(m_pCallback){ - const_cast<SLAudioDeviceCallback*>(m_pCallback)->RecordedDataIsAvailable(pAudioSamples, - nSamples, - BYTES_PER_SAMPLE, - m_bStereoRecording ? 2 : 1, - m_nRecordingSampleRate); - } + if(!pAudioSamples || !nSamples) { + AUDIO_OPENSLES_DEBUG_ERROR("PushRecordingData() - Invalid parameter"); + return; + } + + if(m_pCallback) { + const_cast<SLAudioDeviceCallback*>(m_pCallback)->RecordedDataIsAvailable(pAudioSamples, + nSamples, + BYTES_PER_SAMPLE, + m_bStereoRecording ? 2 : 1, + m_nRecordingSampleRate); + } } #if AUDIO_OPENSLES_UNDER_ANDROID @@ -1004,24 +1033,24 @@ void SLAudioDevice::PlayerSimpleBufferQueueCallback(SLAndroidSimpleBufferQueueIt { SLAudioDevice* This = static_cast<SLAudioDevice*> (pContext); - // AUDIO_OPENSLES_DEBUG_INFO("PlayerSimpleBufferQueueCallback(playing=%s, _playQueueSeq=%d)", (This->m_bPlaying ? "true" : "false"), This->_playQueueSeq); + // AUDIO_OPENSLES_DEBUG_INFO("PlayerSimpleBufferQueueCallback(playing=%s, _playQueueSeq=%d)", (This->m_bPlaying ? "true" : "false"), This->_playQueueSeq); - if (This->m_bPlaying && (This->_playQueueSeq < N_PLAY_QUEUE_BUFFERS)) { - unsigned int noSamp10ms = This->m_nPlayoutSampleRate / 100; + if (This->m_bPlaying && (This->_playQueueSeq < N_PLAY_QUEUE_BUFFERS)) { + unsigned int noSamp10ms = This->m_nPlayoutSampleRate / 100; uint8_t playBuffer[noSamp10ms << BYTES_PER_SAMPLE_LOG2]; uint32_t noSamplesOut = This->PullPlayoutData(playBuffer, noSamp10ms); - + if (noSamp10ms != noSamplesOut) { - if(noSamplesOut){ // (noSamplesOut==0) -> jitter buffer cannot provide data - AUDIO_OPENSLES_DEBUG_ERROR("noSamp10ms (%u) != noSamplesOut (%d)", noSamp10ms, noSamplesOut); - } - noSamplesOut = noSamp10ms; - memset(This->_playQueueBuffer[This->_playQueueSeq], 0, (noSamplesOut << BYTES_PER_SAMPLE_LOG2)); + if(noSamplesOut) { // (noSamplesOut==0) -> jitter buffer cannot provide data + AUDIO_OPENSLES_DEBUG_ERROR("noSamp10ms (%u) != noSamplesOut (%d)", noSamp10ms, noSamplesOut); + } + noSamplesOut = noSamp10ms; + memset(This->_playQueueBuffer[This->_playQueueSeq], 0, (noSamplesOut << BYTES_PER_SAMPLE_LOG2)); } - else{ - memcpy(This->_playQueueBuffer[This->_playQueueSeq], playBuffer, (noSamplesOut << BYTES_PER_SAMPLE_LOG2)); - } - + else { + memcpy(This->_playQueueBuffer[This->_playQueueSeq], playBuffer, (noSamplesOut << BYTES_PER_SAMPLE_LOG2)); + } + SLresult slResult = (*This->m_slPlayerSimpleBufferQueue)->Enqueue(This->m_slPlayerSimpleBufferQueue, This->_playQueueBuffer[This->_playQueueSeq], (noSamplesOut << BYTES_PER_SAMPLE_LOG2)); if (slResult != SL_RESULT_SUCCESS) { AUDIO_OPENSLES_DEBUG_ERROR("Player simpler buffer queue Enqueue failed, noSamplesOut=%d, ret=%d", noSamplesOut, slResult); @@ -1036,31 +1065,30 @@ void SLAudioDevice::RecorderSimpleBufferQueueCallback(SLAndroidSimpleBufferQueue { // AUDIO_OPENSLES_DEBUG_INFO("RecorderSimpleBufferQueueCallback()"); - SLAudioDevice* This = static_cast<SLAudioDevice*> (pContext); + SLAudioDevice* This = static_cast<SLAudioDevice*> (pContext); - if (This->m_bRecording) { + if (This->m_bRecording) { const unsigned int noSamp10ms = This->m_nRecordingSampleRate / 100; - + #if 1 // not using async thread - // push data - This->PushRecordingData(This->_recQueueBuffer[0], noSamp10ms); + // push data + This->PushRecordingData(This->_recQueueBuffer[0], noSamp10ms); // enqueue new buffer SLresult slResult = (*This->m_slRecorderSimpleBufferQueue)->Enqueue( - This->m_slRecorderSimpleBufferQueue, - (void*) This->_recQueueBuffer[0], - (noSamp10ms << BYTES_PER_SAMPLE_LOG2)); + This->m_slRecorderSimpleBufferQueue, + (void*) This->_recQueueBuffer[0], + (noSamp10ms << BYTES_PER_SAMPLE_LOG2)); if (slResult != SL_RESULT_SUCCESS) { - AUDIO_OPENSLES_DEBUG_WARN("Failed to enqueue recording buffer with error code = %d", slResult); + AUDIO_OPENSLES_DEBUG_WARN("Failed to enqueue recording buffer with error code = %d", slResult); return; } #else unsigned int dataPos = 0; - uint16_t bufPos = 0; + uint16_t bufPos = 0; int16_t insertPos = -1; unsigned int nCopy = 0; // Number of samples to copy - while (dataPos < noSamp10ms) - { + while (dataPos < noSamp10ms) { // Loop over all recording buffers or until we find the partially // full buffer // First choice is to insert into partially full buffer, @@ -1068,24 +1096,20 @@ void SLAudioDevice::RecorderSimpleBufferQueueCallback(SLAndroidSimpleBufferQueue bufPos = 0; insertPos = -1; nCopy = 0; - while (bufPos < N_REC_BUFFERS) - { - if ((This->_recLength[bufPos] > 0) && (This->_recLength[bufPos] < noSamp10ms)) - { + while (bufPos < N_REC_BUFFERS) { + if ((This->_recLength[bufPos] > 0) && (This->_recLength[bufPos] < noSamp10ms)) { // Found the partially full buffer insertPos = static_cast<int16_t> (bufPos); bufPos = N_REC_BUFFERS; // Don't need to search more } - else if ((-1 == insertPos) && (0 == This->_recLength[bufPos])) - { + else if ((-1 == insertPos) && (0 == This->_recLength[bufPos])) { // Found an empty buffer insertPos = static_cast<int16_t> (bufPos); } ++bufPos; } - if (insertPos > -1) - { + if (insertPos > -1) { // We found a non-full buffer, copy data from the buffer queue // o recBuffer unsigned int dataToCopy = noSamp10ms - dataPos; @@ -1093,8 +1117,7 @@ void SLAudioDevice::RecorderSimpleBufferQueueCallback(SLAndroidSimpleBufferQueue unsigned int roomInBuffer = noSamp10ms - currentRecLen; nCopy = (dataToCopy < roomInBuffer ? dataToCopy : roomInBuffer); memcpy(&This->_recBuffer[insertPos][currentRecLen], &This->_recQueueBuffer[This->_recQueueSeq][dataPos], nCopy * sizeof(short)); - if (0 == currentRecLen) - { + if (0 == currentRecLen) { _recSeqNumber[insertPos] = This->_recCurrentSeq; ++_recCurrentSeq; } @@ -1104,8 +1127,7 @@ void SLAudioDevice::RecorderSimpleBufferQueueCallback(SLAndroidSimpleBufferQueue This->_recLength[insertPos] += nCopy; dataPos += nCopy; } - else - { + else { // Didn't find a non-full buffer AUDIO_OPENSLES_DEBUG_WARN("Could not insert into recording buffer"); dataPos = noSamp10ms; // Don't try to insert more @@ -1117,19 +1139,19 @@ void SLAudioDevice::RecorderSimpleBufferQueueCallback(SLAndroidSimpleBufferQueue memset(This->_recQueueBuffer[This->_recQueueSeq], 0, (REC_BUF_SIZE_IN_SAMPLES << BYTES_PER_SAMPLE_LOG2)); // write the empty buffer to the queue SLresult slResult = (*This->m_slRecorderSimpleBufferQueue)->Enqueue( - This->m_slRecorderSimpleBufferQueue, - (void*) This->_recQueueBuffer[This->_recQueueSeq], - (noSamp10ms << BYTES_PER_SAMPLE_LOG2)); + This->m_slRecorderSimpleBufferQueue, + (void*) This->_recQueueBuffer[This->_recQueueSeq], + (noSamp10ms << BYTES_PER_SAMPLE_LOG2)); if (slResult != SL_RESULT_SUCCESS) { - AUDIO_OPENSLES_DEBUG_WARN("Failed to enqueue recording buffer with error code = %d", slResult); + AUDIO_OPENSLES_DEBUG_WARN("Failed to enqueue recording buffer with error code = %d", slResult); return; } // update the rec queue seq This->_recQueueSeq = (This->_recQueueSeq + 1) % N_REC_QUEUE_BUFFERS; - // alert thread - // TODO + // alert thread + // TODO #endif } } |