*/
-#include <pthread.h>
#include <assert.h>
#include "CAudioIODef.h"
#include <sound_manager_internal.h>
//LCOV_EXCL_START
CAudioIO::CAudioIO() :
mpPulseAudioClient(nullptr),
- __mMutex(PTHREAD_MUTEX_INITIALIZER),
- __mCondMutex(PTHREAD_MUTEX_INITIALIZER),
- __mCond(PTHREAD_COND_INITIALIZER),
__mIsInit(false) {
mDirection = CAudioInfo::EAudioDirection::AUDIO_DIRECTION_MAX;
mState = CAudioInfo::EAudioIOState::AUDIO_IO_STATE_NONE;
CAudioIO::CAudioIO(CAudioInfo& audioInfo) :
mpPulseAudioClient(nullptr),
- __mMutex(PTHREAD_MUTEX_INITIALIZER),
- __mCondMutex(PTHREAD_MUTEX_INITIALIZER),
- __mCond(PTHREAD_COND_INITIALIZER),
__mIsInit(false) {
mAudioInfo = audioInfo;
mDirection = CAudioInfo::EAudioDirection::AUDIO_DIRECTION_MAX;
mState == CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED));
}
-void CAudioIO::internalLock() {
- if (!__mIsInit)
- THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); //LCOV_EXCL_LINE
-
- if (pthread_mutex_lock(&__mMutex) != 0)
- THROW_ERROR_MSG(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pthread_mutex_lock()"); //LCOV_EXCL_LINE
-
-#ifdef _AUDIO_IO_DEBUG_TIMING_
- AUDIO_IO_LOGD(COLOR_RED "%p LOCKED" COLOR_END, &__mMutex);
-#endif
-}
-
-void CAudioIO::internalUnlock() {
- if (!__mIsInit)
- THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); //LCOV_EXCL_LINE
-
- if (pthread_mutex_unlock(&__mMutex) != 0)
- THROW_ERROR_MSG(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pthread_mutex_lock()"); //LCOV_EXCL_LINE
-
-#ifdef _AUDIO_IO_DEBUG_TIMING_
- AUDIO_IO_LOGD(COLOR_GREEN "%p UNLOCKED" COLOR_END, &__mMutex);
-#endif
-}
-
-void CAudioIO::internalWait() {
- if (!__mIsInit)
- THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); //LCOV_EXCL_LINE
-
-#ifdef _AUDIO_IO_DEBUG_TIMING_
- AUDIO_IO_LOGD(COLOR_RED "WAIT" COLOR_END);
-#endif
- pthread_mutex_lock(&__mCondMutex);
-
- struct timeval now = { 0, };
- struct timeval to_wait = { 0, };
- struct timeval until = { 0, };
- struct timespec until_ts = { 0, };
-
- constexpr int COND_TIMEOUT_MS = 200;
-
- gettimeofday(&now, nullptr);
- to_wait.tv_sec = COND_TIMEOUT_MS / 1000UL;
- to_wait.tv_usec = (COND_TIMEOUT_MS % 1000UL) * 1000UL;
- timeradd(&now, &to_wait, &until);
- until_ts.tv_sec = until.tv_sec;
- until_ts.tv_nsec = until.tv_usec * 1000UL;
-
- int ret = pthread_cond_timedwait(&__mCond, &__mCondMutex, &until_ts);
- if (ret != 0) {
- char str_error[256];
- AUDIO_IO_LOGE("pthread_cond_timedwait error=[%d][%s]", ret, strerror_r(ret, str_error, sizeof(str_error)));
- }
-
- pthread_mutex_unlock(&__mCondMutex);
-}
-
-void CAudioIO::internalSignal() {
- if (!__mIsInit)
- THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); //LCOV_EXCL_LINE
-
-#ifdef _AUDIO_IO_DEBUG_TIMING_
- AUDIO_IO_LOGD(COLOR_GREEN "SIGNAL" COLOR_END);
-#endif
-
- pthread_mutex_lock(&__mCondMutex);
- pthread_cond_signal(&__mCond);
- pthread_mutex_unlock(&__mCondMutex);
-}
-
void CAudioIO::initialize() {
if (__mIsInit)
return;
AUDIO_IO_LOGD("initialize");
-
- int ret = pthread_mutex_init(&__mMutex, NULL);
- if (ret != 0)
- THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pthread_mutex_init()"); //LCOV_EXCL_LINE
-
- ret = pthread_cond_init(&__mCond, NULL);
- if (ret != 0)
- THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pthread_cond_init()"); //LCOV_EXCL_LINE
-
__mIsInit = true;
}
return;
AUDIO_IO_LOGD("finalize");
-
- bool error_occured = false;
- int ret = pthread_mutex_destroy(&__mMutex);
- if (ret != 0) {
-//LCOV_EXCL_START
- AUDIO_IO_LOGE("Failed pthread_mutex_destroy(%p) errno:%d", &__mMutex, ret);
- error_occured = true;
-//LCOV_EXCL_STOP
- }
-
- ret = pthread_mutex_destroy(&__mCondMutex);
- if (ret != 0) {
-//LCOV_EXCL_START
- AUDIO_IO_LOGE("Failed cond pthread_mutex_destroy(%p) errno:%d", &__mCondMutex, ret);
- error_occured = true;
-//LCOV_EXCL_STOP
- }
-
- ret = pthread_cond_destroy(&__mCond);
- if (ret != 0) {
-//LCOV_EXCL_START
- AUDIO_IO_LOGE("Failed pthread_cond_destroy(%p) errno:%d", &__mCond, ret);
- error_occured = true;
-//LCOV_EXCL_STOP
- }
-
- if (error_occured)
- THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Finalize Failed"); //LCOV_EXCL_LINE
-
__mIsInit = false;
}
assert(__mIsInit);
assert(state >= CAudioInfo::EAudioIOState::AUDIO_IO_STATE_NONE && state < CAudioInfo::EAudioIOState::AUDIO_IO_STATE_MAX);
- mStatePrev = mState;
- mState = state;
mByPolicy = byPolicy;
if (mState == mStatePrev)
mStateChangedCallback.onStateChanged(mState, mStatePrev, mByPolicy, mStateChangedCallback.mUserData);
}
+void CAudioIO::setState(CAudioInfo::EAudioIOState state) {
+ mStatePrev = mState;
+ mState = state;
+}
+
void CAudioIO::onStateChanged(CAudioInfo::EAudioIOState state) {
onStateChanged(state, false);
}
if (!__mIsInit || !IsReady())
THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioIO"); //LCOV_EXCL_LINE
- try {
- internalLock();
- AUDIO_IO_LOGD("pause");
- mpPulseAudioClient->cork(true);
- internalUnlock();
- } catch (const CAudioError& e) {
- internalUnlock();
- throw;
- }
+ AUDIO_IO_LOGD("pause");
+ mpPulseAudioClient->cork(true);
}
void CAudioIO::resume() {
if (!__mIsInit || !IsReady())
THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioIO"); //LCOV_EXCL_LINE
- try {
- internalLock();
- AUDIO_IO_LOGD("resume");
- mpPulseAudioClient->cork(false);
- internalUnlock();
- } catch (const CAudioError& e) {
- internalUnlock();
- throw;
- }
+ mpPulseAudioClient->cork(false);
}
+
void CAudioIO::flush() {
if (!__mIsInit || !IsReady())
THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioIO"); //LCOV_EXCL_LINE
- try {
- if (mpPulseAudioClient->isInThread()) {
- mpPulseAudioClient->flush();
- } else {
- internalLock();
- mpPulseAudioClient->flush();
- internalUnlock();
- }
- } catch (const CAudioError& e) {
- if (!mpPulseAudioClient->isInThread())
- internalUnlock();
- throw;
- }
+ mpPulseAudioClient->flush();
}
CAudioInfo& CAudioIO::getAudioInfo() {
if ((errorCode = sound_manager_get_index_from_stream_information(stream_info, &index)) != SOUND_MANAGER_ERROR_NONE)
THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info->index is invalid [ret:%d]", errorCode); //LCOV_EXCL_LINE
getAudioInfo().setAudioIndex(index);
+
+ if (mDirection == CAudioInfo::EAudioDirection::AUDIO_DIRECTION_IN) {
+ int device_id;
+ bool noise_suppression_enabled;
+ sound_acoustic_echo_cancel_type_e aec_type;
+ sound_noise_suppression_type_e ns_type;
+
+ if ((errorCode = sound_manager_get_echo_cancel_reference_device(stream_info, &device_id, &aec_type)) != SOUND_MANAGER_ERROR_NONE)
+ THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Can't get reference device [ret:%d]", errorCode); //LCOV_EXCL_LINE
+
+ if (device_id != SOUND_MANAGER_STREAM_NO_REFERENCE_DEVICE)
+ getAudioInfo().bindEchoCancelReferenceDeviceId(device_id, aec_type);
+
+ if ((errorCode = sound_manager_get_noise_suppression(stream_info, &noise_suppression_enabled, &ns_type)) != SOUND_MANAGER_ERROR_NONE)
+ THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Can't get noise suppression status [ret:%d]", errorCode); //LCOV_EXCL_LINE
+
+ if (noise_suppression_enabled)
+ getAudioInfo().setNoiseSuppression(noise_suppression_enabled, ns_type);
+ }
}