*/
-#include <pthread.h>
#include <assert.h>
#include "CAudioIODef.h"
#include <sound_manager_internal.h>
/**
* class CAudioIO
*/
-//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;
- mStatePrev = CAudioInfo::EAudioIOState::AUDIO_IO_STATE_NONE;
- mByPolicy = false;
-}
-//LCOV_EXCL_STOP
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_NONE;
- mStatePrev = CAudioInfo::EAudioIOState::AUDIO_IO_STATE_NONE;
- mByPolicy = false;
+ mAudioInfo(audioInfo) {
}
void CAudioIO::setInit(bool flag) {
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;
-
- if (pthread_cond_timedwait(&__mCond, &__mCondMutex, &until_ts) != 0) {
- char str_error[256];
- AUDIO_IO_LOGE("pthread_cond_timedwait error=%s", strerror_r(errno, 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;
- }
-}
-
-void CAudioIO::drain() {
- 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->drain();
- } else {
- internalLock();
- mpPulseAudioClient->drain();
- internalUnlock();
- }
- } catch (const CAudioError& e) {
- if (!mpPulseAudioClient->isInThread())
- 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;
+ int method;
+ sound_effect_method_with_reference_e method_reference;
+
+ if (sound_manager_get_effect_method(stream_info, &method) == SOUND_MANAGER_ERROR_NONE)
+ getAudioInfo().setEffectMethod(method);
+
+ if (sound_manager_get_effect_method_with_reference(stream_info, &method_reference, &device_id) == SOUND_MANAGER_ERROR_NONE)
+ getAudioInfo().setEffectMethodWithReference(method_reference, device_id);
+ }
+}
+
+void CAudioIO::onCorked(CPulseAudioClient* pClient, bool corked) {
+ assert(__mIsInit);
+ assert(pClient);
+
+ AUDIO_IO_LOGD("pClient(%p), corked(%d)", pClient, corked);
}