From 3640dbe797b8043586b5cf8f25c13c98c2045296 Mon Sep 17 00:00:00 2001 From: Seungbae Shin Date: Wed, 14 Feb 2018 20:37:08 +0900 Subject: [PATCH] Add session mutex lock - revise try-catch blocks / single line statement - add more capi layer logs for easy debugging [Version] 0.5.2 [Issue Type] Bug Fix Change-Id: Ide35f778da97fe9d69227628d6d97ed607509aec --- include/CAudioIO.h | 4 + include/CAudioIODef.h | 36 ++-- packaging/capi-media-audio-io.spec | 2 +- src/cpp/CAudioIO.cpp | 219 +++++++++++++------------ src/cpp/CAudioInfo.cpp | 4 +- src/cpp/CAudioInput.cpp | 198 +++++++++------------- src/cpp/CAudioOutput.cpp | 174 +++++++++----------- src/cpp/CAudioSessionHandler.cpp | 50 ++---- src/cpp/cpp_audio_io.cpp | 326 ++++++++++++++++++++----------------- 9 files changed, 495 insertions(+), 518 deletions(-) diff --git a/include/CAudioIO.h b/include/CAudioIO.h index 3d48f23..7254d60 100644 --- a/include/CAudioIO.h +++ b/include/CAudioIO.h @@ -113,10 +113,13 @@ namespace tizen_media_audio { void internalLock(); void internalUnlock(); + void internalSessionLock(); + void internalSessionUnlock(); void internalWait(); void internalSignal(); bool isForceIgnore(); + bool isSessionEnabled(); CAudioSessionHandler* mpAudioSessionHandler; CPulseAudioClient* mpPulseAudioClient; @@ -135,6 +138,7 @@ namespace tizen_media_audio { private: pthread_mutex_t __mMutex; + pthread_mutex_t __mSessionMutex; pthread_mutex_t __mCondMutex; pthread_cond_t __mCond; bool __mIsInit; diff --git a/include/CAudioIODef.h b/include/CAudioIODef.h index 71ef83a..0f378df 100644 --- a/include/CAudioIODef.h +++ b/include/CAudioIODef.h @@ -44,17 +44,17 @@ #endif #define LOG_TAG "TIZEN_N_AUDIO_IO" -#define AUDIO_IO_LOGD(_fmt_, arg...) { \ +#define AUDIO_IO_LOGD(_fmt_, arg...) do { \ LOGI(_fmt_, ##arg); \ -} +} while (0) -#define AUDIO_IO_LOGW(_fmt_, arg...) { \ +#define AUDIO_IO_LOGW(_fmt_, arg...) do { \ LOGW(_fmt_, ##arg); \ -} +} while (0) -#define AUDIO_IO_LOGE(_fmt_, arg...) { \ +#define AUDIO_IO_LOGE(_fmt_, arg...) do { \ LOGE(_fmt_, ##arg); \ -} +} while (0) #define _AUDIO_IO_SHELL_COLOR_ #ifdef _AUDIO_IO_SHELL_COLOR_ @@ -81,35 +81,35 @@ #endif -#define RET_ERROR(_x_) {return CAudioError((_x_), __FILE__, __func__, __LINE__);}; -#define RET_ERROR_MSG(_x_, _msg_) {return CAudioError((_x_), (_msg_), __FILE__, __func__, __LINE__);}; +#define RET_ERROR(_x_) do {return CAudioError((_x_), __FILE__, __func__, __LINE__);} while(0) +#define RET_ERROR_MSG(_x_, _msg_) do {return CAudioError((_x_), (_msg_), __FILE__, __func__, __LINE__);} while(0) -#define RET_ERROR_MSG_FORMAT(_x_, _format_, ...) { \ +#define RET_ERROR_MSG_FORMAT(_x_, _format_, ...) do { \ char _msg_[CAudioError::MSG_LENGTH] = {0, }; \ snprintf(_msg_, CAudioError::MSG_LENGTH, _format_, ##__VA_ARGS__); \ return CAudioError((_x_), (_msg_), __FILE__, __func__, __LINE__); \ -}; +} while (0) -#define THROW_ERROR(_x_) {throw CAudioError((_x_), __FILE__, __func__, __LINE__);}; -#define THROW_ERROR_MSG(_x_, _msg_) {throw CAudioError((_x_), (_msg_), __FILE__, __func__, __LINE__);}; +#define THROW_ERROR(_x_) do {throw CAudioError((_x_), __FILE__, __func__, __LINE__);} while (0) +#define THROW_ERROR_MSG(_x_, _msg_) do {throw CAudioError((_x_), (_msg_), __FILE__, __func__, __LINE__);} while (0) -#define THROW_ERROR_MSG_FORMAT(_x_, _format_, ...) { \ +#define THROW_ERROR_MSG_FORMAT(_x_, _format_, ...) do { \ char _msg_[CAudioError::MSG_LENGTH] = {0, }; \ snprintf(_msg_, CAudioError::MSG_LENGTH, _format_, ##__VA_ARGS__); \ throw CAudioError((_x_), (_msg_), __FILE__, __func__, __LINE__); \ -}; +} while (0) #define VALID_POINTER_START(_x_) { \ if ((_x_) != NULL) { #define VALID_POINTER_END } \ } -#define SAFE_DELETE(_x_) {if ((_x_)) {delete (_x_); (_x_) = NULL;}}; -#define SAFE_FINALIZE(_x_) {if ((_x_)) {(_x_)->finalize();}}; -#define SAFE_REMOVE(_x_) {if ((_x_)) {(_x_)->finalize(); delete (_x_); (_x_) = NULL;}}; +#define SAFE_DELETE(_x_) do {if ((_x_)) {delete (_x_); (_x_) = NULL;}} while (0) +#define SAFE_FINALIZE(_x_) do {if ((_x_)) {(_x_)->finalize();}} while (0) +#define SAFE_REMOVE(_x_) do {if ((_x_)) {(_x_)->finalize(); delete (_x_); (_x_) = NULL;}} while (0) #define DEFAULT_PERIOD_SIZE 50 #endif -#endif /* __TIZEN_MEDIA_CPP_OBJECTS_IO_H__ */ +#endif /* __TIZEN_MEDIA_AUDIO_IO_CAUDIO_DEF_H__ */ diff --git a/packaging/capi-media-audio-io.spec b/packaging/capi-media-audio-io.spec index 3cfb628..afd8da2 100644 --- a/packaging/capi-media-audio-io.spec +++ b/packaging/capi-media-audio-io.spec @@ -1,6 +1,6 @@ Name: capi-media-audio-io Summary: An Audio Input & Audio Output library in Tizen Native API -Version: 0.5.1 +Version: 0.5.2 Release: 0 Group: Multimedia/API License: Apache-2.0 diff --git a/src/cpp/CAudioIO.cpp b/src/cpp/CAudioIO.cpp index 0de125b..1d0c36a 100644 --- a/src/cpp/CAudioIO.cpp +++ b/src/cpp/CAudioIO.cpp @@ -32,6 +32,7 @@ CAudioIO::CAudioIO() : mpAudioSessionHandler(NULL), mpPulseAudioClient(NULL), __mMutex(PTHREAD_MUTEX_INITIALIZER), + __mSessionMutex(PTHREAD_MUTEX_INITIALIZER), __mCondMutex(PTHREAD_MUTEX_INITIALIZER), __mCond(PTHREAD_COND_INITIALIZER), __mIsInit(false), @@ -48,6 +49,7 @@ CAudioIO::CAudioIO(CAudioInfo& audioInfo) : mpAudioSessionHandler(NULL), mpPulseAudioClient(NULL), __mMutex(PTHREAD_MUTEX_INITIALIZER), + __mSessionMutex(PTHREAD_MUTEX_INITIALIZER), __mCondMutex(PTHREAD_MUTEX_INITIALIZER), __mCond(PTHREAD_COND_INITIALIZER), __mIsInit(false), @@ -73,17 +75,16 @@ bool CAudioIO::isInit() { } bool CAudioIO::IsReady() { - return ((mState == CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING || mState == CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED)? true : false); + return ((mState == CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING || + mState == CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED)? true : false); } void CAudioIO::internalLock() { - if (__mIsInit == false) { + if (__mIsInit == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); - } - if (pthread_mutex_lock(&__mMutex) != 0) { + if (pthread_mutex_lock(&__mMutex) != 0) THROW_ERROR_MSG(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pthread_mutex_lock()"); - } #ifdef _AUDIO_IO_DEBUG_TIMING_ AUDIO_IO_LOGD(COLOR_RED "%p LOCKED" COLOR_END, &__mMutex); @@ -91,22 +92,45 @@ void CAudioIO::internalLock() { } void CAudioIO::internalUnlock() { - if (__mIsInit == false) { + if (__mIsInit == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); - } - if (pthread_mutex_unlock(&__mMutex) != 0) { + if (pthread_mutex_unlock(&__mMutex) != 0) THROW_ERROR_MSG(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pthread_mutex_lock()"); - } + #ifdef _AUDIO_IO_DEBUG_TIMING_ AUDIO_IO_LOGD(COLOR_GREEN "%p UNLOCKED" COLOR_END, &__mMutex); #endif } +void CAudioIO::internalSessionLock() { + if (__mIsInit == false) + THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); + + if (pthread_mutex_lock(&__mSessionMutex) != 0) + THROW_ERROR_MSG(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed session pthread_mutex_lock()"); + +#ifdef _AUDIO_IO_DEBUG_TIMING_ + AUDIO_IO_LOGD(COLOR_RED "%p LOCKED" COLOR_END, &__mSessionMutex); +#endif +} + +void CAudioIO::internalSessionUnlock() { + if (__mIsInit == false) + THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); + + if (pthread_mutex_unlock(&__mSessionMutex) != 0) + THROW_ERROR_MSG(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed session pthread_mutex_unlock()"); + +#ifdef _AUDIO_IO_DEBUG_TIMING_ + AUDIO_IO_LOGD(COLOR_GREEN "%p UNLOCKED" COLOR_END, &__mSessionMutex); +#endif +} + + void CAudioIO::internalWait() { - if (__mIsInit == false) { + if (__mIsInit == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); - } #ifdef _AUDIO_IO_DEBUG_TIMING_ AUDIO_IO_LOGD(COLOR_RED "WAIT" COLOR_END); @@ -118,9 +142,8 @@ void CAudioIO::internalWait() { } void CAudioIO::internalSignal() { - if (__mIsInit == false) { + if (__mIsInit == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); - } #ifdef _AUDIO_IO_DEBUG_TIMING_ AUDIO_IO_LOGD(COLOR_GREEN "SIGNAL" COLOR_END); @@ -135,30 +158,39 @@ bool CAudioIO::isForceIgnore() { return __mForceIgnore; } +bool CAudioIO::isSessionEnabled() { + if (mpAudioSessionHandler && + mpAudioSessionHandler->isSkipSession() == false && + mpAudioSessionHandler->getId() >= 0) + return true; + + return false; +} + void CAudioIO::initialize() { - if (__mIsInit == true) { + if (__mIsInit == true) return; - } AUDIO_IO_LOGD("initialize"); int ret = pthread_mutex_init(&__mMutex, NULL); - if (ret != 0) { + if (ret != 0) THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pthread_mutex_init()"); - } + + ret = pthread_mutex_init(&__mSessionMutex, NULL); + if (ret != 0) + THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed session pthread_mutex_init()"); ret = pthread_cond_init(&__mCond, NULL); - if (ret != 0) { + if (ret != 0) THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pthread_cond_init()"); - } __mIsInit = true; } void CAudioIO::finalize() { - if (__mIsInit == false) { + if (__mIsInit == false) return; - } AUDIO_IO_LOGD("finalize"); @@ -168,12 +200,18 @@ void CAudioIO::finalize() { AUDIO_IO_LOGE("Failed pthread_mutex_destroy(%p) errno:%d", &__mMutex, ret); error_occured = true; } - ret = pthread_mutex_destroy(&__mCondMutex); + + ret = pthread_mutex_destroy(&__mSessionMutex); if (ret != 0) { - AUDIO_IO_LOGE("Failed pthread_mutex_destroy(%p) errno:%d", &__mCondMutex, ret); + AUDIO_IO_LOGE("Failed session pthread_mutex_destroy(%p) errno:%d", &__mSessionMutex, ret); error_occured = true; } + ret = pthread_mutex_destroy(&__mCondMutex); + if (ret != 0) { + AUDIO_IO_LOGE("Failed cond pthread_mutex_destroy(%p) errno:%d", &__mCondMutex, ret); + error_occured = true; + } ret = pthread_cond_destroy(&__mCond); if (ret != 0) { @@ -196,9 +234,8 @@ void CAudioIO::onStream(CPulseAudioClient* pClient, size_t length) { AUDIO_IO_LOGD("mStreamCallback.onStream(%p), pClient(%p), length(%zu)", mStreamCallback.onStream, pClient, length); #endif - if (mStreamCallback.onStream != NULL) { + if (mStreamCallback.onStream != NULL) mStreamCallback.onStream(length, mStreamCallback.mUserData); - } } void CAudioIO::onStateChanged(CAudioInfo::EAudioIOState state, bool byPolicy) { @@ -206,8 +243,8 @@ void CAudioIO::onStateChanged(CAudioInfo::EAudioIOState state, bool byPolicy) { assert(state >= CAudioInfo::EAudioIOState::AUDIO_IO_STATE_NONE && state < CAudioInfo::EAudioIOState::AUDIO_IO_STATE_MAX); mStatePrev = mState; - mState = state; - mByPolicy = byPolicy; + mState = state; + mByPolicy = byPolicy; if (mState == mStatePrev) return; @@ -221,9 +258,8 @@ void CAudioIO::onStateChanged(CAudioInfo::EAudioIOState state, bool byPolicy) { static_cast(mState), mByPolicy); - if (mStateChangedCallback.onStateChanged != NULL) { + if (mStateChangedCallback.onStateChanged != NULL) mStateChangedCallback.onStateChanged(mState, mStatePrev, mByPolicy, mStateChangedCallback.mUserData); - } } void CAudioIO::onStateChanged(CAudioInfo::EAudioIOState state) { @@ -272,9 +308,8 @@ void CAudioIO::onInterrupt(CAudioSessionHandler* pHandler, int id, mm_sound_focu } else if (state == FOCUS_IS_ACQUIRED) { // Focus handle(id) of the other application was acquired, do pause if possible internalLock(); - if (mpPulseAudioClient) { + if (mpPulseAudioClient) mpPulseAudioClient->cork(true); - } mIsInterrupted = true; internalUnlock(); @@ -286,9 +321,8 @@ void CAudioIO::onInterrupt(CAudioSessionHandler* pHandler, int id, mm_sound_focu // Triggered by 'focus callback' /////////////////////////////////////// - if (pHandler->getId() != id) { + if (pHandler->getId() != id) AUDIO_IO_LOGW("Id is different, why? [mId : %d]", pHandler->getId()); - } if (session_option & MM_SESSION_OPTION_UNINTERRUPTIBLE) { AUDIO_IO_LOGD("Session option is uninterruptible, skip..."); @@ -298,9 +332,8 @@ void CAudioIO::onInterrupt(CAudioSessionHandler* pHandler, int id, mm_sound_focu if (state == FOCUS_IS_RELEASED) { // Focus handle(id) was released, do pause here internalLock(); - if (mpPulseAudioClient) { + if (mpPulseAudioClient) mpPulseAudioClient->cork(true); - } mIsInterrupted = true; internalUnlock(); @@ -339,54 +372,41 @@ void CAudioIO::onSignal(CAudioSessionHandler* pHandler, mm_sound_signal_name_t s } void CAudioIO::prepare() { - if (__mIsInit == false) { + if (__mIsInit == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); - } - if (mDirection == CAudioInfo::EAudioDirection::AUDIO_DIRECTION_IN) { - AUDIO_IO_LOGD("Prepare for Audio in"); - } else if (mDirection == CAudioInfo::EAudioDirection::AUDIO_DIRECTION_OUT) { - AUDIO_IO_LOGD("Prepare for Audio Out"); - } + try { if (mIsInterrupted) { + internalSessionLock(); AUDIO_IO_LOGE("This is preparing during interrupted!!!"); - bool isSkip = mpAudioSessionHandler->isSkipSession(); - if (__mForceIgnore == false && isSkip == false && mpAudioSessionHandler->getId() >= 0) { + if (isSessionEnabled() && __mForceIgnore == false) { AUDIO_IO_LOGE("Session updatePlaying!!!"); mpAudioSessionHandler->updatePlaying(); } - - if (mpPulseAudioClient && mpPulseAudioClient->isCorked()) { - AUDIO_IO_LOGE("Uncork!"); - mpPulseAudioClient->cork(false); - } - mIsInterrupted = false; + internalSessionUnlock(); } - - AUDIO_IO_LOGD("------> prepare done"); - /* Do nothing */ } catch (CAudioError& e) { + internalSessionUnlock(); throw; } + + if (mIsInterrupted) { + if (mpPulseAudioClient && mpPulseAudioClient->isCorked()) { + AUDIO_IO_LOGE("Uncork!"); + mpPulseAudioClient->cork(false); + } + mIsInterrupted = false; + } } void CAudioIO::unprepare() { - if (__mIsInit == false) { + if (__mIsInit == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); - } - - try { - AUDIO_IO_LOGD("unprepare ----->"); - /* Do nothing */ - } catch (CAudioError& e) { - throw; - } } void CAudioIO::pause() { - if (__mIsInit == false || IsReady() == false) { + if (__mIsInit == false || IsReady() == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioIO"); - } try { internalLock(); @@ -400,9 +420,8 @@ void CAudioIO::pause() { } void CAudioIO::resume() { - if (__mIsInit == false || IsReady() == false) { + if (__mIsInit == false || IsReady() == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioIO"); - } try { internalLock(); @@ -416,9 +435,8 @@ void CAudioIO::resume() { } void CAudioIO::drain() { - if (__mIsInit == false || IsReady() == false) { + if (__mIsInit == false || IsReady() == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioIO"); - } try { if (mpPulseAudioClient->isInThread()) { @@ -429,17 +447,15 @@ void CAudioIO::drain() { internalUnlock(); } } catch (CAudioError& e) { - if (!mpPulseAudioClient->isInThread()) { + if (!mpPulseAudioClient->isInThread()) internalUnlock(); - } throw; } } void CAudioIO::flush() { - if (__mIsInit == false || IsReady() == false) { + if (__mIsInit == false || IsReady() == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioIO"); - } try { if (mpPulseAudioClient->isInThread()) { @@ -450,65 +466,57 @@ void CAudioIO::flush() { internalUnlock(); } } catch (CAudioError& e) { - if (!mpPulseAudioClient->isInThread()) { + if (!mpPulseAudioClient->isInThread()) internalUnlock(); - } throw; } } CAudioInfo& CAudioIO::getAudioInfo() { - if (__mIsInit == false) { + if (__mIsInit == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); - } return mAudioInfo; } void CAudioIO::setStreamCallback(SStreamCallback callback) { - if (__mIsInit == false) { + if (__mIsInit == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); - } mStreamCallback = callback; } CAudioIO::SStreamCallback CAudioIO::getStreamCallback() { - if (__mIsInit == false) { + if (__mIsInit == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); - } return mStreamCallback; } void CAudioIO::setStateChangedCallback(SStateChangedCallback callback) { - if (__mIsInit == false) { + if (__mIsInit == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); - } mStateChangedCallback = callback; } CAudioIO::SStateChangedCallback CAudioIO::getStateChangedCallback() { - if (__mIsInit == false) { + if (__mIsInit == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); - } return mStateChangedCallback; } void CAudioIO::setInterruptCallback(SInterruptCallback callback) { - if (__mIsInit == false) { + if (__mIsInit == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); - } mInterruptCallback = callback; } CAudioIO::SInterruptCallback CAudioIO::getInterruptCallback() { - if (__mIsInit == false) { + if (__mIsInit == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); - } return mInterruptCallback; } @@ -519,12 +527,10 @@ void CAudioIO::ignoreSession() { try { internalLock(); - if (mpPulseAudioClient != NULL && mState == CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING) THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "An Operation is not permitted while started"); abandonInternalFocus(); - internalUnlock(); } catch (CAudioError& e) { internalUnlock(); @@ -553,13 +559,11 @@ void CAudioIO::setStreamInfo(sound_stream_info_h stream_info) { if ((errorCode = sound_manager_is_available_stream_information(stream_info, NATIVE_API_AUDIO_IO, &avail)) != SOUND_MANAGER_ERROR_NONE) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info is invalid [ret:%d]", errorCode); - if (!avail) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE, "Input stream is not supported"); if ((errorCode = sound_manager_get_type_from_stream_information(stream_info, &type)) != SOUND_MANAGER_ERROR_NONE) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter stream_info->stream_type is invalid [ret:%d]", errorCode); - if (mDirection == CAudioInfo::EAudioDirection::AUDIO_DIRECTION_IN) getAudioInfo().convertInputStreamType2AudioType(type, &audioType); else @@ -568,10 +572,8 @@ void CAudioIO::setStreamInfo(sound_stream_info_h stream_info) { 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); - getAudioInfo().setAudioIndex(index); - AUDIO_IO_LOGD("stream info(%p) is set", stream_info); } catch (CAudioError& e) { throw; } @@ -581,33 +583,40 @@ void CAudioIO::setInternalStreamInfo() { if (__mIsInit == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); - try { - if (mState != CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE) - THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_STATE, "it is not permitted while started"); + if (mState != CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE) + THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_STATE, "it is not permitted while started"); + sound_stream_info_h stream_info = NULL; + + try { + internalSessionLock(); if (mpAudioSessionHandler && mpAudioSessionHandler->getMultimediaSession() == MM_SESSION_TYPE_VOIP) { - sound_stream_info_h stream_info = NULL; mpAudioSessionHandler->getInternalVoipStreamInfo(&stream_info); AUDIO_IO_LOGD("get internal VOIP stream info(%p)", stream_info); - setStreamInfo(stream_info); } + internalSessionUnlock(); } catch (CAudioError& e) { + internalSessionUnlock(); throw; } + + /* NOTE: to avoid double-lock, this should be outside of try-catch. */ + if (stream_info) + setStreamInfo(stream_info); } void CAudioIO::abandonInternalFocus() { - bool isSkip = mpAudioSessionHandler->isSkipSession(); - int id = mpAudioSessionHandler->getId(); - try { - if (isSkip == false && id >= 0) + internalSessionLock(); + if (isSessionEnabled()) { mpAudioSessionHandler->unregisterSound(); - - mpAudioSessionHandler->finalize(); + mpAudioSessionHandler->finalize(); /* FIXME : SAFE_FINALIZE or SAFE_DELETE? */ + } __mForceIgnore = true; + internalSessionUnlock(); } catch (CAudioError& e) { + internalSessionUnlock(); throw; } } diff --git a/src/cpp/CAudioInfo.cpp b/src/cpp/CAudioInfo.cpp index 8b7f40e..503a1c5 100644 --- a/src/cpp/CAudioInfo.cpp +++ b/src/cpp/CAudioInfo.cpp @@ -122,10 +122,10 @@ int CAudioInfo::getSampleSize() { } void CAudioInfo::convertAudioType2StreamType(CAudioInfo::EAudioType audioType, char **streamType) { - if (audioType < CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA || audioType >= CAudioInfo::EAudioType::AUDIO_TYPE_MAX) { + if (audioType < CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA || audioType >= CAudioInfo::EAudioType::AUDIO_TYPE_MAX) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE, "The audioType is not supported [audioType:%u]", to_integral(audioType)); - } + *streamType = (char *)__STREAM_TYPE_TABLE[(unsigned int)audioType]; return; } diff --git a/src/cpp/CAudioInput.cpp b/src/cpp/CAudioInput.cpp index 59eeb40..9a07ad7 100644 --- a/src/cpp/CAudioInput.cpp +++ b/src/cpp/CAudioInput.cpp @@ -84,12 +84,14 @@ void CAudioInput::onInterrupt(CAudioSessionHandler* pHandler, int id, mm_sound_f assert(pHandler); AUDIO_IO_LOGD("[pHandler:%p], [focus_type:%d], [state:%d], [reason_for_change:%s], [additional_info:%s]", pHandler, focus_type, state, reason_for_change, additional_info); + CAudioIO::onInterrupt(pHandler, id, focus_type, state, reason_for_change, additional_info); } void CAudioInput::onSignal(CAudioSessionHandler* pHandler, mm_sound_signal_name_t signal, int value) { assert(pHandler); AUDIO_IO_LOGD("[pHandler:%p], [signal:%d], [value:%d]", pHandler, signal, value); + CAudioIO::onSignal(pHandler, signal, value); } @@ -150,6 +152,7 @@ static bool __IsPrivilegeAllowed() { prData.paMainloop = pa_threaded_mainloop_new(); if (prData.paMainloop == NULL) THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_threaded_mainloop_new()"); + c = pa_context_new(pa_threaded_mainloop_get_api(prData.paMainloop), CLIENT_NAME); if (c == NULL) THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_context_new()"); @@ -203,31 +206,36 @@ static bool __IsPrivilegeAllowed() { } void CAudioInput::initialize() { - if (__IsInit() == true) { + if (__IsInit() == true) return; - } try { CAudioIO::initialize(); - if (__IsPrivilegeAllowed() == false) { - THROW_ERROR_MSG(CAudioError::EError::ERROR_PERMISSION_DENIED, "No privilege for record"); - } + } catch (CAudioError& e) { + finalize(); + throw; + } - // Create ASM Handler - mpAudioSessionHandler = new CAudioSessionHandler(CAudioSessionHandler::EAudioSessionType::AUDIO_SESSION_TYPE_CAPTURE, mAudioInfo, this); + if (__IsPrivilegeAllowed() == false) + THROW_ERROR_MSG(CAudioError::EError::ERROR_PERMISSION_DENIED, "No privilege for record"); - // Initialize ASM Handler + try { + internalSessionLock(); + mpAudioSessionHandler = new CAudioSessionHandler(CAudioSessionHandler::EAudioSessionType::AUDIO_SESSION_TYPE_CAPTURE, mAudioInfo, this); mpAudioSessionHandler->initialize(); - - __setInit(true); - CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE); + internalSessionUnlock(); } catch (CAudioError& e) { + internalSessionUnlock(); finalize(); throw; } catch (const std::bad_alloc&) { + internalSessionUnlock(); finalize(); THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed to allocate CAudioSessionHandler object"); } + + __setInit(true); + CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE); } void CAudioInput::finalize() { @@ -236,8 +244,10 @@ void CAudioInput::finalize() { return; } + internalSessionLock(); SAFE_FINALIZE(mpAudioSessionHandler); SAFE_DELETE(mpAudioSessionHandler); + internalSessionUnlock(); CAudioIO::finalize(); @@ -245,9 +255,8 @@ void CAudioInput::finalize() { } void CAudioInput::prepare() { - if (__IsInit() == false) { + if (__IsInit() == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CAudioInput"); - } if (__IsReady() == true) { AUDIO_IO_LOGD("Already prepared CAudioInput"); @@ -257,25 +266,36 @@ void CAudioInput::prepare() { /* Check invalid AudioType */ CAudioInfo::EAudioType audioType = mAudioInfo.getAudioType(); - if (audioType < CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA || audioType >= CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA) { + if (audioType < CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA || + audioType >= CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "The audioType is invalid [type:%d]", static_cast(audioType)); - } try { - if (mpAudioSessionHandler->getId() < 0) { - if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSession() == false) { - /* Register ASM Listener */ + internalSessionLock(); + if (mpAudioSessionHandler && + mpAudioSessionHandler->getId() < 0) { // Did not registerSound() + if (isForceIgnore() == false && + mpAudioSessionHandler->isSkipSession() == false) { AUDIO_IO_LOGD("Register ASM Listener"); mpAudioSessionHandler->registerSound(); } } + internalSessionUnlock(); + } catch (CAudioError& e) { + internalSessionUnlock(); + throw; + } - CAudioIO::setInternalStreamInfo(); + CAudioIO::setInternalStreamInfo(); - if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSession() == false) + try { + internalSessionLock(); + if (isSessionEnabled() && isForceIgnore() == false) mpAudioSessionHandler->updatePlaying(); + internalSessionUnlock(); } catch (CAudioError& e) { + internalSessionUnlock(); throw; } @@ -286,17 +306,12 @@ void CAudioInput::prepare() { CPulseStreamSpec spec(streamSpec, mAudioInfo); internalLock(); - - /* Create PulseAudio Handler */ mpPulseAudioClient = new CPulseAudioClient(CPulseAudioClient::EStreamDirection::STREAM_DIRECTION_RECORD, spec, this); - - /* Initialize PulseAudio Handler */ mpPulseAudioClient->initialize(); #ifndef DISABLE_MOBILE_BACK_COMP /* Uncork stream which is created with CORKED flag */ mpPulseAudioClient->cork(false); #endif - internalUnlock(); CAudioIO::prepare(); @@ -312,24 +327,21 @@ void CAudioInput::prepare() { } void CAudioInput::unprepare() { - if (__IsInit() == false) { + if (__IsInit() == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CAudioInput"); - } if (__IsReady() == false) { AUDIO_IO_LOGD("Already unprepared"); return; } - try { - CAudioIO::unprepare(); + CAudioIO::unprepare(); + try { internalLock(); - SAFE_FINALIZE(mpPulseAudioClient); SAFE_DELETE(mpPulseAudioClient); - internalUnlock(); } catch (CAudioError& e) { internalUnlock(); @@ -337,64 +349,48 @@ void CAudioInput::unprepare() { } try { - if (mpAudioSessionHandler->getId() >= 0) { - if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSession() == false) - mpAudioSessionHandler->updateStop(); - } + internalSessionLock(); + if (isSessionEnabled() && isForceIgnore() == false) + mpAudioSessionHandler->updateStop(); + internalSessionUnlock(); CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE); } catch (CAudioError& e) { - internalUnlock(); + internalSessionUnlock(); throw; } } void CAudioInput::pause() { - if (__IsInit() == false || __IsReady() == false) { + if (__IsInit() == false || __IsReady() == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioInput"); - } - if (CAudioIO::getState() != CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING) { + if (CAudioIO::getState() != CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING) THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_STATE, "Can't pause if not in Running state"); - } - if (mpPulseAudioClient->isInThread() == true) { + if (mpPulseAudioClient->isInThread() == true) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_OPERATION, "Can't pause in thread"); - } - - try { - CAudioIO::pause(); - CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED); - } catch (CAudioError& e) { - throw; - } + CAudioIO::pause(); + CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED); } void CAudioInput::resume() { - if (__IsInit() == false || __IsReady() == false) { + if (__IsInit() == false || __IsReady() == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioInput"); - } - if (CAudioIO::getState() != CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED) { + if (CAudioIO::getState() != CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED) THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_STATE, "Can't resume if not in Paused state"); - } - if (mpPulseAudioClient->isInThread() == true) { + if (mpPulseAudioClient->isInThread() == true) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_OPERATION, "Can't resume in thread"); - } - - try { - CAudioIO::resume(); - CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING); - } catch (CAudioError& e) { - throw; - } + CAudioIO::resume(); + CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING); } void CAudioInput::drain() { @@ -402,69 +398,58 @@ void CAudioInput::drain() { } void CAudioInput::flush() { - if (__IsInit() == false || __IsReady() == false) { + if (__IsInit() == false || __IsReady() == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioInput"); - } - try { - CAudioIO::flush(); - } catch (CAudioError& e) { - throw; - } + CAudioIO::flush(); } int CAudioInput::getBufferSize() { - if (__IsInit() == false) { + if (__IsInit() == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CAudioInput"); - } /* FIXME : return calculated size here to satisfy backward compatibility */ return (mAudioInfo.getSampleRate() * DEFAULT_PERIOD_SIZE) / 1000 * mAudioInfo.getSampleSize(); } void CAudioInput::setStreamCallback(SStreamCallback callback) { - if (__IsInit() == false) { + if (__IsInit() == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CAudioInput"); - } - if (callback.onStream == NULL) { - AUDIO_IO_LOGD("__mIsUsedSyncRead = true"); + if (callback.onStream == NULL) __mIsUsedSyncRead = true; - } else { - AUDIO_IO_LOGD("__mIsUsedSyncRead = false"); + else __mIsUsedSyncRead = false; - } + AUDIO_IO_LOGD("__mIsUsedSyncRead = %d", __mIsUsedSyncRead); CAudioIO::setStreamCallback(callback); } size_t CAudioInput::read(void* buffer, size_t length) { - if (__IsInit() == false || __IsReady() == false) { + if (__IsInit() == false || __IsReady() == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioInput"); - } - if (buffer == NULL) { + + if (buffer == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL buffer:%p", buffer); - } - if (CAudioIO::getState() != CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING) { + + if (CAudioIO::getState() != CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING) THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "Can't read if not in Running state"); - } + /* Checks synchronous flag */ - if (__mIsUsedSyncRead == false) { + if (__mIsUsedSyncRead == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "Invalid operation of read() if receive stream callback"); - } int ret = 0; try { internalLock(); - if (mIsInterrupted) { + if (mIsInterrupted) THROW_ERROR_MSG(CAudioError::EError::ERROR_POLICY_BLOCKED, "audio-io is interrupted"); - } // If another thread did call unprepare, do not read if (mpPulseAudioClient == NULL) @@ -473,8 +458,8 @@ size_t CAudioInput::read(void* buffer, size_t length) { // Block until read done ret = mpPulseAudioClient->read(buffer, length); - internalUnlock(); + sched_yield(); } catch (CAudioError& e) { internalUnlock(); @@ -485,52 +470,31 @@ size_t CAudioInput::read(void* buffer, size_t length) { } int CAudioInput::peek(const void** buffer, size_t* length) { - if (__IsInit() == false || __IsReady() == false) { + if (__IsInit() == false || __IsReady() == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioInput"); - } - if (buffer == NULL || length == NULL) { + if (buffer == NULL || length == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL buffer:%p, length:%p", buffer, length); - } /* Checks synchronous flag */ - if (__mIsUsedSyncRead == true) { + if (__mIsUsedSyncRead == true) THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "Invalid operation of peek() if does not receive a stream callback"); - } - - int ret = 0; - try { - ret = mpPulseAudioClient->peek(buffer, length); - } catch (CAudioError& e) { - throw; - } - - return ret; + return mpPulseAudioClient->peek(buffer, length); } int CAudioInput::drop() { - if (__IsInit() == false || __IsReady() == false) { + if (__IsInit() == false || __IsReady() == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioInput"); - } /* Checks synchronous flag */ - if (__mIsUsedSyncRead == true) { + if (__mIsUsedSyncRead == true) THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "Invalid operation of drop() if does not receive a stream callback"); - } - int ret = 0; - - try { - ret = mpPulseAudioClient->drop(); - } catch (CAudioError& e) { - throw; - } - - return ret; + return mpPulseAudioClient->drop(); } diff --git a/src/cpp/CAudioOutput.cpp b/src/cpp/CAudioOutput.cpp index f96f78f..c2ece71 100644 --- a/src/cpp/CAudioOutput.cpp +++ b/src/cpp/CAudioOutput.cpp @@ -98,28 +98,33 @@ bool CAudioOutput::__IsReady() { } void CAudioOutput::initialize() { - if (__IsInit() == true) { + if (__IsInit() == true) return; - } try { CAudioIO::initialize(); + } catch (CAudioError& e) { + finalize(); + throw; + } - // Create ASM Handler + try { + internalSessionLock(); mpAudioSessionHandler = new CAudioSessionHandler(CAudioSessionHandler::EAudioSessionType::AUDIO_SESSION_TYPE_PLAYBACK, mAudioInfo, this); - - // Initialize ASM Handler mpAudioSessionHandler->initialize(); - - __setInit(true); - CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE); + internalSessionUnlock(); } catch (CAudioError& e) { + internalSessionUnlock(); finalize(); throw; } catch (const std::bad_alloc&) { + internalSessionUnlock(); finalize(); THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed to allocate CAudioSessionHandler object"); } + + __setInit(true); + CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE); } void CAudioOutput::finalize() { @@ -128,8 +133,10 @@ void CAudioOutput::finalize() { return; } + internalSessionLock(); SAFE_FINALIZE(mpAudioSessionHandler); SAFE_DELETE(mpAudioSessionHandler); + internalSessionUnlock(); CAudioIO::finalize(); @@ -137,9 +144,8 @@ void CAudioOutput::finalize() { } void CAudioOutput::prepare() { - if (__IsInit() == false) { + if (__IsInit() == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CAudioOutput"); - } if (__IsReady() == true) { AUDIO_IO_LOGD("Already prepared CAudioOutput"); @@ -149,25 +155,36 @@ void CAudioOutput::prepare() { /* Check invalid AudioType */ CAudioInfo::EAudioType audioType = mAudioInfo.getAudioType(); - if (audioType < CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA || audioType >= CAudioInfo::EAudioType::AUDIO_TYPE_MAX) { + if (audioType < CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA || + audioType >= CAudioInfo::EAudioType::AUDIO_TYPE_MAX) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "The audioType is invalid [type:%d]", static_cast(audioType)); - } try { - if (mpAudioSessionHandler->getId() < 0) { // Did not registerSound() - if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSession() == false) { - /* Register ASM Listener */ + internalSessionLock(); + if (mpAudioSessionHandler && + mpAudioSessionHandler->getId() < 0) { // Did not registerSound() + if (isForceIgnore() == false && + mpAudioSessionHandler->isSkipSession() == false) { AUDIO_IO_LOGD("Register ASM Listener"); mpAudioSessionHandler->registerSound(); } } + internalSessionUnlock(); + } catch (CAudioError& e) { + internalSessionUnlock(); + throw; + } - CAudioIO::setInternalStreamInfo(); + CAudioIO::setInternalStreamInfo(); - if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSession() == false) + try { + internalSessionLock(); + if (isSessionEnabled() && isForceIgnore() == false) mpAudioSessionHandler->updatePlaying(); + internalSessionUnlock(); } catch (CAudioError& e) { + internalSessionUnlock(); throw; } @@ -186,11 +203,7 @@ void CAudioOutput::prepare() { CPulseStreamSpec spec(streamSpec, mAudioInfo); internalLock(); - - /* Create PulseAudio Handler */ mpPulseAudioClient = new CPulseAudioClient(CPulseAudioClient::EStreamDirection::STREAM_DIRECTION_PLAYBACK, spec, this); - - /* Initialize PulseAudio Handler */ mpPulseAudioClient->initialize(); #ifndef DISABLE_MOBILE_BACK_COMP /* Uncork stream which is created with CORKED flag */ @@ -211,10 +224,9 @@ void CAudioOutput::prepare() { } void CAudioOutput::unprepare() { - if (__IsInit() == false) { + if (__IsInit() == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CAudioOutput"); - } if (__IsReady() == false) { AUDIO_IO_LOGD("Already unprepared"); @@ -222,161 +234,134 @@ void CAudioOutput::unprepare() { } try { - if (mpAudioSessionHandler->getId() >= 0 && !mIsInterrupted) { - if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSession() == false) { - if (mStreamCallback.onStream == NULL) - CAudioIO::drain(); - } + internalSessionLock(); + if (isSessionEnabled() && + !mIsInterrupted && + !isForceIgnore() && + mStreamCallback.onStream == NULL) { + CAudioIO::drain(); } - CAudioIO::unprepare(); + internalSessionUnlock(); + } catch (CAudioError& e) { + internalSessionUnlock(); + throw; + } - internalLock(); + CAudioIO::unprepare(); + try { + internalLock(); SAFE_FINALIZE(mpPulseAudioClient); SAFE_DELETE(mpPulseAudioClient); - internalUnlock(); - } catch (CAudioError& e) { internalUnlock(); throw; } try { - if (mpAudioSessionHandler->getId() >= 0) { - if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSession() == false) - mpAudioSessionHandler->updateStop(); - } + internalSessionLock(); + if (isSessionEnabled() && isForceIgnore() == false) + mpAudioSessionHandler->updateStop(); + internalSessionUnlock(); + CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE); } catch (CAudioError& e) { - internalUnlock(); + internalSessionUnlock(); throw; } } void CAudioOutput::pause() { - if (__IsInit() == false || __IsReady() == false) { + if (__IsInit() == false || __IsReady() == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioOutput"); - } - if (CAudioIO::getState() != CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING) { + if (CAudioIO::getState() != CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING) THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_STATE, "Can't pause if not in Running state"); - } - if (mpPulseAudioClient->isInThread() == true) { + if (mpPulseAudioClient->isInThread() == true) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_OPERATION, "Can't pause in thread"); - } - try { - CAudioIO::pause(); - - CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED); - } catch (CAudioError& e) { - throw; - } + CAudioIO::pause(); + CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED); } void CAudioOutput::resume() { - if (__IsInit() == false || __IsReady() == false) { + if (__IsInit() == false || __IsReady() == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioOutput"); - } - if (CAudioIO::getState() != CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED) { + if (CAudioIO::getState() != CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED) THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_STATE, "Can't resume if not in Paused state"); - } - if (mpPulseAudioClient->isInThread() == true) { + if (mpPulseAudioClient->isInThread() == true) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_OPERATION, "Can't resume in thread"); - } - - try { - CAudioIO::resume(); - CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING); - } catch (CAudioError& e) { - throw; - } + CAudioIO::resume(); + CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING); } void CAudioOutput::drain() { - if (__IsInit() == false || __IsReady() == false) { + if (__IsInit() == false || __IsReady() == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioOutput"); - } if (mStreamCallback.onStream) THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "async type don't support drain"); - try { - CAudioIO::drain(); - } catch (CAudioError& e) { - throw; - } + CAudioIO::drain(); } void CAudioOutput::flush() { - if (__IsInit() == false || __IsReady() == false) { + if (__IsInit() == false || __IsReady() == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioOutput"); - } - try { - CAudioIO::flush(); - } catch (CAudioError& e) { - throw; - } + CAudioIO::flush(); } int CAudioOutput::getBufferSize() { - if (__IsInit() == false) { + if (__IsInit() == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioOutput"); - } /* FIXME : return calculated size here to satisfy backward compatibility */ return (mAudioInfo.getSampleRate() * DEFAULT_PERIOD_SIZE) / 1000 * mAudioInfo.getSampleSize(); } size_t CAudioOutput::write(const void* buffer, size_t length) { - if (__IsInit() == false || __IsReady() == false) { + if (__IsInit() == false || __IsReady() == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioOutput"); - } - if (buffer == NULL) { + if (buffer == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are invalid - buffer:%p, length:%zu", buffer, length); - } - if (CAudioIO::getState() != CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING) { + + if (CAudioIO::getState() != CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING) THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "Can't write if not in Running state"); - } /* When write() is called in PulseAudio callback, bypass a pcm data to CPulseAudioClient (For Asynchronous) */ if (mpPulseAudioClient && mpPulseAudioClient->isInThread() == true) { int ret = mpPulseAudioClient->write(buffer, length); - if (ret < 0) { + if (ret < 0) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "The written result is invalid ret:%d", ret); - } - #ifdef _AUDIO_IO_DEBUG_TIMING_ AUDIO_IO_LOGD("CPulseAudioClient->write(buffer:%p, length:%d)", buffer, length); #endif - return length; } try { /* For synchronization */ internalLock(); - if (mIsInterrupted) { + if (mIsInterrupted) THROW_ERROR_MSG(CAudioError::EError::ERROR_POLICY_BLOCKED, "audio io is interrupted"); - } // If another thread did call unprepare, do not write if (mpPulseAudioClient == NULL) @@ -398,19 +383,17 @@ size_t CAudioOutput::write(const void* buffer, size_t length) { internalWait(); } - if (l > lengthIter) { + if (l > lengthIter) l = lengthIter; - } #ifdef _AUDIO_IO_DEBUG_TIMING_ AUDIO_IO_LOGD("CPulseAudioClient->write(buffer:%p, length:%d)", buffer, l); #endif int ret = mpPulseAudioClient->write(buffer, l); - if (ret < 0) { + if (ret < 0) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "The written result is invalid ret:%d", ret); - } buffer = static_cast(buffer) + l; lengthIter -= l; @@ -418,6 +401,7 @@ size_t CAudioOutput::write(const void* buffer, size_t length) { __mIsUsedSyncWrite = false; internalUnlock(); + sched_yield(); } catch (CAudioError& e) { __mIsUsedSyncWrite = false; diff --git a/src/cpp/CAudioSessionHandler.cpp b/src/cpp/CAudioSessionHandler.cpp index d11898b..ceaf3ce 100644 --- a/src/cpp/CAudioSessionHandler.cpp +++ b/src/cpp/CAudioSessionHandler.cpp @@ -82,36 +82,32 @@ int CAudioSessionHandler::__focusIdCountGet() { } void CAudioSessionHandler::__lockFocusIdMutex() { - if (pthread_mutex_lock(&__mFocusIdMutex) != 0) { + if (pthread_mutex_lock(&__mFocusIdMutex) != 0) THROW_ERROR_MSG(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pthread_mutex_lock() - FocusId Mutex"); - } #ifdef _AUDIO_IO_DEBUG_TIMING_ AUDIO_IO_LOGD(COLOR_RED "LOCK - FocusId Mutex" COLOR_END); #endif } void CAudioSessionHandler::__unlockFocusIdMutex() { - if (pthread_mutex_unlock(&__mFocusIdMutex) != 0) { + if (pthread_mutex_unlock(&__mFocusIdMutex) != 0) THROW_ERROR_MSG(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pthread_mutex_unlock() - FocusId Mutex"); - } #ifdef _AUDIO_IO_DEBUG_TIMING_ AUDIO_IO_LOGD(COLOR_GREEN "UNLOCK - FocusId Mutex" COLOR_END); #endif } void CAudioSessionHandler::__lockFocusCBMutex() { - if (pthread_mutex_lock(&__mFocusCBMutex) != 0) { + if (pthread_mutex_lock(&__mFocusCBMutex) != 0) THROW_ERROR_MSG(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pthread_mutex_lock() - FocusCB Mutex"); - } #ifdef _AUDIO_IO_DEBUG_TIMING_ AUDIO_IO_LOGD(COLOR_RED "LOCK - FocusCB Mutex" COLOR_END); #endif } void CAudioSessionHandler::__unlockFocusCBMutex() { - if (pthread_mutex_unlock(&__mFocusCBMutex) != 0) { + if (pthread_mutex_unlock(&__mFocusCBMutex) != 0) THROW_ERROR_MSG(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pthread_mutex_unlock() - FocusCB Mutex"); - } #ifdef _AUDIO_IO_DEBUG_TIMING_ AUDIO_IO_LOGD(COLOR_GREEN "UNLOCK - FocusCB Mutex" COLOR_END); #endif @@ -176,11 +172,10 @@ CAudioError CAudioSessionHandler::__getAsmInformation(MMSessionType *type, int * /* Read session information */ int ret = 0; if ((ret = _mm_session_util_read_information(-1, (int*)¤tSession, &sessionOptions)) < 0) { - if (ret == (int) MM_ERROR_INVALID_HANDLE) { + if (ret == (int) MM_ERROR_INVALID_HANDLE) RET_ERROR_MSG(CAudioError::EError::ERROR_INVALID_HANDLE, "Failed _mm_session_util_read_information(). Invalid handle"); - } else { + else RET_ERROR_MSG(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed _mm_session_util_read_information(). Not exist"); - } } *type = currentSession; @@ -241,11 +236,10 @@ void CAudioSessionHandler::getInternalVoipStreamInfo(sound_stream_info_h *stream #if 0 int ret; if ((ret = sound_manager_get_internal_voip_stream_information(stream_info))) { - if (ret == SOUND_MANAGER_ERROR_NO_DATA) { + if (ret == SOUND_MANAGER_ERROR_NO_DATA) AUDIO_IO_LOGW("there's no internal voip stream info."); - } else { + else AUDIO_IO_LOGE("failed to sound_manager_get_internal_voip_stream_information(), ret(0x%x)", ret); - } } #endif return; @@ -271,9 +265,8 @@ void CAudioSessionHandler::__sound_pcm_signal_cb(mm_sound_signal_name_t signal, void CAudioSessionHandler::initialize() { AUDIO_IO_LOGD(""); - if (__mIsInit == true) { + if (__mIsInit == true) return; - } MMSessionType currentSession = MM_SESSION_TYPE_MEDIA; int sessionOptions = 0; // Mix with others by default @@ -294,10 +287,9 @@ void CAudioSessionHandler::initialize() { // Use focus watch callback with signal subscribe int errorCode = mm_sound_subscribe_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &__mSubscribeId, __sound_pcm_signal_cb, static_cast(this)); - if (errorCode != MM_ERROR_NONE || __mSubscribeId == 0) { + if (errorCode != MM_ERROR_NONE || __mSubscribeId == 0) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed mm_sound_subscribe_signal() err:0x%x, __mSubscribeId:%u", errorCode, __mSubscribeId); - } AUDIO_IO_LOGD("Subscribed mm_sound signal [id:%d]", __mSubscribeId); @@ -322,22 +314,19 @@ void CAudioSessionHandler::initialize() { __mMultimediaSession = currentSession; __mOptions = sessionOptions; - if (this->__mAudioSession == EAudioSessionType::AUDIO_SESSION_TYPE_CAPTURE) { + if (this->__mAudioSession == EAudioSessionType::AUDIO_SESSION_TYPE_CAPTURE) __pcmCaptureCountInc(); - } __mIsInit = true; } void CAudioSessionHandler::finalize() { AUDIO_IO_LOGD(""); - if (__mIsInit == false) { + if (__mIsInit == false) return; - } - if (__mAudioSession == EAudioSessionType::AUDIO_SESSION_TYPE_CAPTURE) { + if (__mAudioSession == EAudioSessionType::AUDIO_SESSION_TYPE_CAPTURE) __pcmCaptureCountDec(); - } unregisterSound(); @@ -404,9 +393,8 @@ void CAudioSessionHandler::__sound_pcm_focus_watch_cb(int id, mm_sound_focus_typ } void CAudioSessionHandler::registerSound() { - if (__mIsInit == false) { + if (__mIsInit == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioSessionHandler"); - } if (__mUseFocus == true) { __lockFocusIdMutex(); @@ -471,9 +459,8 @@ void CAudioSessionHandler::registerSound() { } void CAudioSessionHandler::unregisterSound() { - if (__mIsInit == false) { + if (__mIsInit == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioSessionHandler"); - } __lockFocusIdMutex(); if (__mUseFocus == true && __mId >= 0) { @@ -510,9 +497,9 @@ void CAudioSessionHandler::unregisterSound() { } void CAudioSessionHandler::updatePlaying() { - if (__mIsInit == false) { + if (__mIsInit == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioSessionHandler"); - } + __lockFocusIdMutex(); if (!__mUseFocus || __mId < 0) { __unlockFocusIdMutex(); @@ -584,9 +571,8 @@ void CAudioSessionHandler::updatePlaying() { } void CAudioSessionHandler::updateStop() { - if (__mIsInit == false) { + if (__mIsInit == false) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioSessionHandler"); - } if (__mUseFocus && __isFocusRequired(__mMultimediaSession, __mOptions)) { __lockFocusIdMutex(); diff --git a/src/cpp/cpp_audio_io.cpp b/src/cpp/cpp_audio_io.cpp index 110d5c3..b3d12b7 100644 --- a/src/cpp/cpp_audio_io.cpp +++ b/src/cpp/cpp_audio_io.cpp @@ -82,10 +82,10 @@ typedef struct audio_io_state_changed_cb_s { * The CAudioIO is a abstract class object about Input and Output */ typedef struct audio_io_s { - CAudioIO* audioIoHandle; - audio_io_interrupted_cb_s interrupt_callback; - audio_io_stream_cb_s stream_callback; - audio_io_state_changed_cb_s state_changed_callback; + CAudioIO* audioIoHandle; + audio_io_interrupted_cb_s interrupt_callback; + audio_io_stream_cb_s stream_callback; + audio_io_state_changed_cb_s state_changed_callback; audio_io_s() : audioIoHandle(NULL) {/* Empty Body */} @@ -96,7 +96,7 @@ typedef struct audio_io_s { * Internal functions */ static audio_io_error_e __convert_CAudioError(CAudioError& error) { - audio_io_error_e ret = AUDIO_IO_ERROR_NONE; + audio_io_error_e ret = AUDIO_IO_ERROR_NONE; CAudioError::EError err = error.getError(); switch (err) { @@ -300,31 +300,27 @@ static audio_io_state_e __convert_state_type(const CAudioInfo::EAudioIOState src } static void __check_audio_param(int sample_rate, audio_channel_e channel, audio_sample_type_e type) { - if (sample_rate < 0) { + if (sample_rate < 0) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid sample rate :%d", sample_rate); - } - if (channel != AUDIO_CHANNEL_MONO && channel != AUDIO_CHANNEL_STEREO) { + if (channel != AUDIO_CHANNEL_MONO && channel != AUDIO_CHANNEL_STEREO) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid channel :%d", channel); - } - if (type != AUDIO_SAMPLE_TYPE_U8 && type != AUDIO_SAMPLE_TYPE_S16_LE) { + if (type != AUDIO_SAMPLE_TYPE_U8 && type != AUDIO_SAMPLE_TYPE_S16_LE) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid sample type :%d", type); - } } static void __check_audio_param(int sample_rate, audio_channel_e channel, audio_sample_type_e type, sound_type_e sound_type) { __check_audio_param(sample_rate, channel, type); - if (sound_type < SOUND_TYPE_SYSTEM || sound_type > SOUND_TYPE_VOICE) { + if (sound_type < SOUND_TYPE_SYSTEM || sound_type > SOUND_TYPE_VOICE) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Invalid sound type : %d", sound_type); - } } static CAudioInfo __generate_audio_input_info(int sampleRate, audio_channel_e channel, audio_sample_type_e sample_type) { - CAudioInfo::EChannel dstChannel; + CAudioInfo::EChannel dstChannel; CAudioInfo::ESampleType dstSampleType; - CAudioInfo::EAudioType dstAudioType = CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA; + CAudioInfo::EAudioType dstAudioType = CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA; __convert_channel_2_audio_info_channel(channel, dstChannel); __convert_sample_type_2_audio_info_sample_type(sample_type, dstSampleType); @@ -333,9 +329,9 @@ static CAudioInfo __generate_audio_input_info(int sampleRate, audio_channel_e ch } static CAudioInfo __generate_audio_input_loopback_info(int sampleRate, audio_channel_e channel, audio_sample_type_e sample_type) { - CAudioInfo::EChannel dstChannel; + CAudioInfo::EChannel dstChannel; CAudioInfo::ESampleType dstSampleType; - CAudioInfo::EAudioType dstAudioType = CAudioInfo::EAudioType::AUDIO_IN_TYPE_LOOPBACK; + CAudioInfo::EAudioType dstAudioType = CAudioInfo::EAudioType::AUDIO_IN_TYPE_LOOPBACK; __convert_channel_2_audio_info_channel(channel, dstChannel); __convert_sample_type_2_audio_info_sample_type(sample_type, dstSampleType); @@ -344,9 +340,9 @@ static CAudioInfo __generate_audio_input_loopback_info(int sampleRate, audio_cha } static CAudioInfo __generate_audio_output_info(int sampleRate, audio_channel_e channel, audio_sample_type_e sample_type, sound_type_e sound_type) { - CAudioInfo::EChannel dstChannel; + CAudioInfo::EChannel dstChannel; CAudioInfo::ESampleType dstSampleType; - CAudioInfo::EAudioType dstAudioType; + CAudioInfo::EAudioType dstAudioType; __convert_channel_2_audio_info_channel(channel, dstChannel); __convert_sample_type_2_audio_info_sample_type(sample_type, dstSampleType); @@ -408,6 +404,8 @@ int cpp_audio_in_create(int sample_rate, audio_channel_e channel, audio_sample_t __check_audio_param(sample_rate, channel, type); + AUDIO_IO_LOGD("samplerate:[%d] channel:[0x%x] sample_type:[0x%x]", sample_rate, channel, type); + /* If MIC is not supported, return NOT_SUPPORTED error */ ret = system_info_get_platform_bool(FEATURE_MICROPHONE, &mic_enable); AUDIO_IO_LOGD("system_info_platform [%s]=[%d], ret[%d]", FEATURE_MICROPHONE, mic_enable, ret); @@ -415,14 +413,13 @@ int cpp_audio_in_create(int sample_rate, audio_channel_e channel, audio_sample_t THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "System doesn't support microphone!"); } - handle = new audio_io_s; - CAudioInfo audioInfo = __generate_audio_input_info(sample_rate, channel, type); + handle = new audio_io_s; handle->audioIoHandle = new CAudioInput(audioInfo); - handle->audioIoHandle->initialize(); + AUDIO_IO_LOGD("[%p] created", handle); *input = handle; } catch (CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); @@ -441,19 +438,15 @@ int cpp_audio_in_create(int sample_rate, audio_channel_e channel, audio_sample_t int cpp_audio_in_create_loopback(int sample_rate, audio_channel_e channel, audio_sample_type_e type , audio_in_h* input) { audio_io_s* handle = NULL; try { - if (input == NULL) { + if (input == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input); - } __check_audio_param(sample_rate, channel, type); - - handle = new audio_io_s; - CAudioInfo audioInfo = __generate_audio_input_loopback_info(sample_rate, channel, type); + handle = new audio_io_s; handle->audioIoHandle = new CAudioInput(audioInfo); - handle->audioIoHandle->initialize(); *input = handle; @@ -475,11 +468,11 @@ int cpp_audio_in_destroy(audio_in_h input) { audio_io_s* handle = static_cast(input); try { - if (handle == NULL) { + if (handle == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input); - } assert(handle->audioIoHandle); + AUDIO_IO_LOGD("[%p]", handle); /* Internal unprepare for backward compatibility */ handle->audioIoHandle->unprepare(); @@ -492,6 +485,8 @@ int cpp_audio_in_destroy(audio_in_h input) { return __convert_CAudioError(e); } + AUDIO_IO_LOGD("destroyed"); + return AUDIO_IO_ERROR_NONE; } @@ -499,11 +494,11 @@ int cpp_audio_in_set_sound_stream_info(audio_in_h input, sound_stream_info_h str audio_io_s* handle = static_cast(input); try { - if (handle == NULL || stream_info == NULL) { + if (handle == NULL || stream_info == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, stream_info:%p", input, stream_info); - } assert(handle->audioIoHandle); + AUDIO_IO_LOGD("[%p], stream_info:[%p]", handle, stream_info); handle->audioIoHandle->setStreamInfo(stream_info); } catch (CAudioError& e) { @@ -511,6 +506,8 @@ int cpp_audio_in_set_sound_stream_info(audio_in_h input, sound_stream_info_h str return __convert_CAudioError(e); } + AUDIO_IO_LOGD("[%p] done", handle); + return AUDIO_IO_ERROR_NONE; } @@ -518,11 +515,11 @@ int cpp_audio_in_prepare(audio_in_h input) { audio_io_s* handle = static_cast(input); try { - if (handle == NULL) { + if (handle == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input); - } assert(handle->audioIoHandle); + AUDIO_IO_LOGD("[%p]", handle); handle->audioIoHandle->prepare(); } catch (CAudioError& e) { @@ -530,6 +527,8 @@ int cpp_audio_in_prepare(audio_in_h input) { return __convert_CAudioError(e); } + AUDIO_IO_LOGD("[%p] prepared", handle); + return AUDIO_IO_ERROR_NONE; } @@ -537,11 +536,11 @@ int cpp_audio_in_unprepare(audio_in_h input) { audio_io_s* handle = static_cast(input); try { - if (handle == NULL) { + if (handle == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input); - } assert(handle->audioIoHandle); + AUDIO_IO_LOGD("[%p]", handle); handle->audioIoHandle->unprepare(); } catch (CAudioError& e) { @@ -549,6 +548,8 @@ int cpp_audio_in_unprepare(audio_in_h input) { return __convert_CAudioError(e); } + AUDIO_IO_LOGD("[%p] unprepared", handle); + return AUDIO_IO_ERROR_NONE; } @@ -556,11 +557,11 @@ int cpp_audio_in_pause(audio_in_h input) { audio_io_s* handle = static_cast(input); try { - if (handle == NULL) { + if (handle == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input); - } assert(handle->audioIoHandle); + AUDIO_IO_LOGD("[%p]", handle); handle->audioIoHandle->pause(); } catch (CAudioError& e) { @@ -568,6 +569,8 @@ int cpp_audio_in_pause(audio_in_h input) { return __convert_CAudioError(e); } + AUDIO_IO_LOGD("[%p] paused", handle); + return AUDIO_IO_ERROR_NONE; } @@ -575,11 +578,11 @@ int cpp_audio_in_resume(audio_in_h input) { audio_io_s* handle = static_cast(input); try { - if (handle == NULL) { + if (handle == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input); - } assert(handle->audioIoHandle); + AUDIO_IO_LOGD("[%p]", handle); handle->audioIoHandle->resume(); } catch (CAudioError& e) { @@ -587,6 +590,8 @@ int cpp_audio_in_resume(audio_in_h input) { return __convert_CAudioError(e); } + AUDIO_IO_LOGD("[%p] resumed", handle); + return AUDIO_IO_ERROR_NONE; } @@ -594,11 +599,11 @@ int cpp_audio_in_drain(audio_in_h input) { audio_io_s* handle = static_cast(input); try { - if (handle == NULL) { + if (handle == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input); - } assert(handle->audioIoHandle); + AUDIO_IO_LOGD("[%p]", handle); handle->audioIoHandle->drain(); } catch (CAudioError& e) { @@ -606,6 +611,8 @@ int cpp_audio_in_drain(audio_in_h input) { return __convert_CAudioError(e); } + AUDIO_IO_LOGD("[%p] drained", handle); + return AUDIO_IO_ERROR_NONE; } @@ -613,11 +620,11 @@ int cpp_audio_in_flush(audio_in_h input) { audio_io_s* handle = static_cast(input); try { - if (handle == NULL) { + if (handle == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input); - } assert(handle->audioIoHandle); + AUDIO_IO_LOGD("[%p]", handle); handle->audioIoHandle->flush(); } catch (CAudioError& e) { @@ -625,6 +632,8 @@ int cpp_audio_in_flush(audio_in_h input) { return __convert_CAudioError(e); } + AUDIO_IO_LOGD("[%p] flushed", handle); + return AUDIO_IO_ERROR_NONE; } @@ -633,10 +642,9 @@ int cpp_audio_in_read(audio_in_h input, void *buffer, unsigned int length) { int ret = 0; try { - if (handle == NULL || buffer == NULL) { + if (handle == NULL || buffer == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, buffer:%p", input, buffer); - } assert(handle->audioIoHandle); CAudioInput* inputHandle = static_cast(handle->audioIoHandle); @@ -661,10 +669,9 @@ int cpp_audio_in_get_buffer_size(audio_in_h input, int *size) { audio_io_s* handle = static_cast(input); try { - if (handle == NULL || size == NULL) { + if (handle == NULL || size == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, size:%p", input, size); - } assert(handle->audioIoHandle); CAudioIO* inputHandle = static_cast(handle->audioIoHandle); @@ -684,10 +691,9 @@ int cpp_audio_in_get_sample_rate(audio_in_h input, int *sample_rate) { audio_io_s* handle = static_cast(input); try { - if (handle == NULL || sample_rate == NULL) { + if (handle == NULL || sample_rate == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, sample_rate:%p", input, sample_rate); - } assert(handle->audioIoHandle); *sample_rate = handle->audioIoHandle->getAudioInfo().getSampleRate(); @@ -703,10 +709,9 @@ int cpp_audio_in_get_channel(audio_in_h input, audio_channel_e *channel) { audio_io_s* handle = static_cast(input); try { - if (handle == NULL || channel == NULL) { + if (handle == NULL || channel == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, channel:%p", input, channel); - } assert(handle->audioIoHandle); const CAudioInfo::EChannel srcChannel = handle->audioIoHandle->getAudioInfo().getChannel(); @@ -726,10 +731,9 @@ int cpp_audio_in_get_sample_type(audio_in_h input, audio_sample_type_e *type) { audio_io_s* handle = static_cast(input); try { - if (handle == NULL || type == NULL) { + if (handle == NULL || type == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, type:%p", input, type); - } assert(handle->audioIoHandle); const CAudioInfo::ESampleType srcSampleType = handle->audioIoHandle->getAudioInfo().getSampleType(); @@ -751,20 +755,21 @@ static void __interrupt_cb_internal(IAudioSessionEventListener::EInterruptCode _ assert(handle); - if (handle->interrupt_callback.onInterrupt) { + AUDIO_IO_LOGD("Calling interrupted callback for handle:[%p] code:[%d]", handle, code); + + if (handle->interrupt_callback.onInterrupt) handle->interrupt_callback.onInterrupt(code, handle->interrupt_callback.user_data); - } } int cpp_audio_in_set_interrupted_cb(audio_in_h input, audio_io_interrupted_cb callback, void *user_data) { audio_io_s* handle = static_cast(input); try { - if (handle == NULL || callback == NULL) { + if (handle == NULL || callback == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, callback:%p", input, callback); - } assert(handle->audioIoHandle); + AUDIO_IO_LOGD("[%p], callback:[%p], user_data:[%p]", handle, callback, user_data); handle->interrupt_callback.onInterrupt = callback; handle->interrupt_callback.user_data = user_data; @@ -779,6 +784,8 @@ int cpp_audio_in_set_interrupted_cb(audio_in_h input, audio_io_interrupted_cb ca return __convert_CAudioError(e); } + AUDIO_IO_LOGD("[%p] done", handle); + return AUDIO_IO_ERROR_NONE; } @@ -786,11 +793,11 @@ int cpp_audio_in_unset_interrupted_cb(audio_in_h input) { audio_io_s* handle = static_cast(input); try { - if (handle == NULL) { + if (handle == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input); - } assert(handle->audioIoHandle); + AUDIO_IO_LOGD("[%p]", handle); handle->interrupt_callback.onInterrupt = NULL; handle->interrupt_callback.user_data = NULL; @@ -805,6 +812,8 @@ int cpp_audio_in_unset_interrupted_cb(audio_in_h input) { return __convert_CAudioError(e); } + AUDIO_IO_LOGD("[%p] done", handle); + return AUDIO_IO_ERROR_NONE; } @@ -812,16 +821,14 @@ int cpp_audio_in_ignore_session(audio_in_h input) { audio_io_s* handle = static_cast(input); try { - if (handle == NULL) { + if (handle == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input); - } - - if (handle->stream_callback.onStream) { + if (handle->stream_callback.onStream) THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "Not support ignore session in async mode"); - } assert(handle->audioIoHandle); + AUDIO_IO_LOGD("[%p]", handle); handle->audioIoHandle->ignoreSession(); } catch (CAudioError& e) { @@ -829,6 +836,8 @@ int cpp_audio_in_ignore_session(audio_in_h input) { return __convert_CAudioError(e); } + AUDIO_IO_LOGD("[%p] done", handle); + return AUDIO_IO_ERROR_NONE; } @@ -836,9 +845,8 @@ static void __stream_cb_internal(size_t nbytes, void *user_data) { audio_io_s* audioIo = static_cast(user_data); assert(audioIo); - if (audioIo->stream_callback.onStream) { + if (audioIo->stream_callback.onStream) audioIo->stream_callback.onStream(audioIo, nbytes, audioIo->stream_callback.user_data); - } } static void __state_changed_cb_internal(CAudioInfo::EAudioIOState state, @@ -848,22 +856,21 @@ static void __state_changed_cb_internal(CAudioInfo::EAudioIOState state, audio_io_s* audioIo = static_cast(user_data); assert(audioIo); - if (audioIo->state_changed_callback.onStateChanged) { + if (audioIo->state_changed_callback.onStateChanged) audioIo->state_changed_callback.onStateChanged(audioIo, __convert_state_type(state_prev), __convert_state_type(state), by_policy, audioIo->state_changed_callback.user_data); - } } int cpp_audio_in_set_stream_cb(audio_in_h input, audio_in_stream_cb callback, void* user_data) { audio_io_s* handle = static_cast(input); try { - if (handle == NULL || callback == NULL) { + if (handle == NULL || callback == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, callback:%p", input, callback); - } assert(handle->audioIoHandle); + AUDIO_IO_LOGD("[%p], callback:[%p], user_data:[%p]", handle, callback, user_data); handle->stream_callback.onStream = callback; handle->stream_callback.user_data = user_data; @@ -878,6 +885,8 @@ int cpp_audio_in_set_stream_cb(audio_in_h input, audio_in_stream_cb callback, vo return __convert_CAudioError(e); } + AUDIO_IO_LOGD("[%p] done", handle); + return AUDIO_IO_ERROR_NONE; } @@ -885,11 +894,11 @@ int cpp_audio_in_unset_stream_cb(audio_in_h input) { audio_io_s* handle = static_cast(input); try { - if (handle == NULL) { + if (handle == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input); - } assert(handle->audioIoHandle); + AUDIO_IO_LOGD("[%p]", handle); handle->stream_callback.onStream = NULL; handle->stream_callback.user_data = NULL; @@ -904,6 +913,8 @@ int cpp_audio_in_unset_stream_cb(audio_in_h input) { return __convert_CAudioError(e); } + AUDIO_IO_LOGD("[%p] done", handle); + return AUDIO_IO_ERROR_NONE; } @@ -912,15 +923,13 @@ int cpp_audio_in_peek(audio_in_h input, const void **buffer, unsigned int *lengt size_t _length = 0; try { - if (handle == NULL || buffer == NULL) { + if (handle == NULL || buffer == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, buffer:%p", input, buffer); - } CAudioInput* inputHandle = static_cast(handle->audioIoHandle); - if (inputHandle == NULL) { + if (inputHandle == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_HANDLE, "Handle is NULL"); - } inputHandle->peek(buffer, &_length); } catch (CAudioError& e) { @@ -937,15 +946,13 @@ int cpp_audio_in_drop(audio_in_h input) { audio_io_s* handle = static_cast(input); try { - if (handle == NULL) { + if (handle == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p", input); - } CAudioInput* inputHandle = static_cast(handle->audioIoHandle); - if (inputHandle == NULL) { + if (inputHandle == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_HANDLE, "Handle is NULL"); - } inputHandle->drop(); } catch (CAudioError& e) { @@ -960,11 +967,11 @@ int cpp_audio_in_set_state_changed_cb(audio_in_h input, audio_in_state_changed_c audio_io_s* handle = static_cast(input); try { - if (handle == NULL || callback == NULL) { + if (handle == NULL || callback == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL input:%p, callback:%p", input, callback); - } assert(handle->audioIoHandle); + AUDIO_IO_LOGD("[%p], callback:[%p], user_data:[%p]", handle, callback, user_data); handle->state_changed_callback.onStateChanged = callback; handle->state_changed_callback.user_data = user_data; @@ -979,6 +986,8 @@ int cpp_audio_in_set_state_changed_cb(audio_in_h input, audio_in_state_changed_c return __convert_CAudioError(e); } + AUDIO_IO_LOGD("[%p] done", handle); + return AUDIO_IO_ERROR_NONE; } @@ -986,11 +995,11 @@ int cpp_audio_in_unset_state_changed_cb(audio_in_h input) { audio_io_s* handle = static_cast(input); try { - if (handle == NULL) { + if (handle == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", input); - } assert(handle->audioIoHandle); + AUDIO_IO_LOGD("[%p]", handle); handle->state_changed_callback.onStateChanged = NULL; handle->state_changed_callback.user_data = NULL; @@ -1005,6 +1014,8 @@ int cpp_audio_in_unset_state_changed_cb(audio_in_h input) { return __convert_CAudioError(e); } + AUDIO_IO_LOGD("[%p] done", handle); + return AUDIO_IO_ERROR_NONE; } @@ -1015,21 +1026,20 @@ int cpp_audio_in_unset_state_changed_cb(audio_in_h input) { int cpp_audio_out_create(int sample_rate, audio_channel_e channel, audio_sample_type_e type, sound_type_e sound_type, audio_out_h *output) { audio_io_s* handle = NULL; try { - if (output == NULL) { + if (output == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output); - } __check_audio_param(sample_rate, channel, type, sound_type); - handle = new audio_io_s; - + AUDIO_IO_LOGD("samplerate:[%d] channel:[0x%x] sample_type:[0x%x] sound_type:[%d]", sample_rate, channel, type, sound_type); CAudioInfo audioInfo = __generate_audio_output_info(sample_rate, channel, type, sound_type); + handle = new audio_io_s; handle->audioIoHandle = new CAudioOutput(audioInfo); - handle->audioIoHandle->initialize(); + AUDIO_IO_LOGD("[%p] created", handle); *output = handle; } catch (CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); @@ -1048,21 +1058,20 @@ int cpp_audio_out_create(int sample_rate, audio_channel_e channel, audio_sample_ int cpp_audio_out_create_new(int sample_rate, audio_channel_e channel, audio_sample_type_e type, audio_out_h *output) { audio_io_s* handle = NULL; try { - if (output == NULL) { + if (output == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output); - } __check_audio_param(sample_rate, channel, type, SOUND_TYPE_SYSTEM /*default check */); - handle = new audio_io_s; - + AUDIO_IO_LOGD("samplerate:[%d] channel:[0x%x] sample_type:[0x%x]", sample_rate, channel, type); CAudioInfo audioInfo = __generate_audio_output_info(sample_rate, channel, type, SOUND_TYPE_MEDIA); + handle = new audio_io_s; handle->audioIoHandle = new CAudioOutput(audioInfo); - handle->audioIoHandle->initialize(); + AUDIO_IO_LOGD("[%p] created", handle); *output = handle; } catch (CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); @@ -1082,11 +1091,11 @@ int cpp_audio_out_destroy(audio_out_h output) { audio_io_s* handle = static_cast(output); try { - if (handle == NULL) { + if (handle == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output); - } assert(handle->audioIoHandle); + AUDIO_IO_LOGD("[%p]", handle); /* Internal unprepare for backward compatibility */ handle->audioIoHandle->unprepare(); @@ -1099,6 +1108,8 @@ int cpp_audio_out_destroy(audio_out_h output) { return __convert_CAudioError(e); } + AUDIO_IO_LOGD("destroyed"); + return AUDIO_IO_ERROR_NONE; } @@ -1106,11 +1117,11 @@ int cpp_audio_out_set_sound_stream_info(audio_out_h output, sound_stream_info_h audio_io_s* handle = static_cast(output); try { - if (handle == NULL || stream_info == NULL) { + if (handle == NULL || stream_info == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, stream_info:%p", output, stream_info); - } assert(handle->audioIoHandle); + AUDIO_IO_LOGD("[%p], stream_info:[%p]", handle, stream_info); handle->audioIoHandle->setStreamInfo(stream_info); } catch (CAudioError& e) { @@ -1118,6 +1129,8 @@ int cpp_audio_out_set_sound_stream_info(audio_out_h output, sound_stream_info_h return __convert_CAudioError(e); } + AUDIO_IO_LOGD("[%p] done", handle); + return AUDIO_IO_ERROR_NONE; } @@ -1125,11 +1138,11 @@ int cpp_audio_out_prepare(audio_out_h output) { audio_io_s* handle = static_cast(output); try { - if (handle == NULL) { + if (handle == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output); - } assert(handle->audioIoHandle); + AUDIO_IO_LOGD("[%p]", handle); handle->audioIoHandle->prepare(); } catch (CAudioError& e) { @@ -1137,6 +1150,8 @@ int cpp_audio_out_prepare(audio_out_h output) { return __convert_CAudioError(e); } + AUDIO_IO_LOGD("[%p] prepared", handle); + return AUDIO_IO_ERROR_NONE; } @@ -1144,11 +1159,11 @@ int cpp_audio_out_unprepare(audio_out_h output) { audio_io_s* handle = static_cast(output); try { - if (handle == NULL) { + if (handle == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output); - } assert(handle->audioIoHandle); + AUDIO_IO_LOGD("[%p]", handle); handle->audioIoHandle->unprepare(); } catch (CAudioError& e) { @@ -1156,6 +1171,8 @@ int cpp_audio_out_unprepare(audio_out_h output) { return __convert_CAudioError(e); } + AUDIO_IO_LOGD("[%p] unprepared", handle); + return AUDIO_IO_ERROR_NONE; } @@ -1163,11 +1180,11 @@ int cpp_audio_out_pause(audio_out_h output) { audio_io_s* handle = static_cast(output); try { - if (handle == NULL) { + if (handle == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output); - } assert(handle->audioIoHandle); + AUDIO_IO_LOGD("[%p]", handle); handle->audioIoHandle->pause(); } catch (CAudioError& e) { @@ -1175,6 +1192,8 @@ int cpp_audio_out_pause(audio_out_h output) { return __convert_CAudioError(e); } + AUDIO_IO_LOGD("[%p] paused", handle); + return AUDIO_IO_ERROR_NONE; } @@ -1182,11 +1201,11 @@ int cpp_audio_out_resume(audio_out_h output) { audio_io_s* handle = static_cast(output); try { - if (handle == NULL) { + if (handle == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output); - } assert(handle->audioIoHandle); + AUDIO_IO_LOGD("[%p]", handle); handle->audioIoHandle->resume(); } catch (CAudioError& e) { @@ -1194,6 +1213,8 @@ int cpp_audio_out_resume(audio_out_h output) { return __convert_CAudioError(e); } + AUDIO_IO_LOGD("[%p] resumed", handle); + return AUDIO_IO_ERROR_NONE; } @@ -1201,11 +1222,11 @@ int cpp_audio_out_drain(audio_out_h output) { audio_io_s* handle = static_cast(output); try { - if (handle == NULL) { + if (handle == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output); - } assert(handle->audioIoHandle); + AUDIO_IO_LOGD("[%p]", handle); handle->audioIoHandle->drain(); } catch (CAudioError& e) { @@ -1213,6 +1234,8 @@ int cpp_audio_out_drain(audio_out_h output) { return __convert_CAudioError(e); } + AUDIO_IO_LOGD("[%p] drained", handle); + return AUDIO_IO_ERROR_NONE; } @@ -1220,11 +1243,11 @@ int cpp_audio_out_flush(audio_out_h output) { audio_io_s* handle = static_cast(output); try { - if (handle == NULL) { + if (handle == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p", output); - } assert(handle->audioIoHandle); + AUDIO_IO_LOGD("[%p]", handle); handle->audioIoHandle->flush(); } catch (CAudioError& e) { @@ -1232,6 +1255,8 @@ int cpp_audio_out_flush(audio_out_h output) { return __convert_CAudioError(e); } + AUDIO_IO_LOGD("[%p] flushed", handle); + return AUDIO_IO_ERROR_NONE; } @@ -1240,16 +1265,14 @@ int cpp_audio_out_write(audio_out_h output, void *buffer, unsigned int length) { int ret = 0; try { - if (handle == NULL || buffer == NULL) { + if (handle == NULL || buffer == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameter is NULL output:%p, buffer:%p", output, buffer); - } assert(handle->audioIoHandle); CAudioOutput* outputHandle = static_cast(handle->audioIoHandle); - if (outputHandle == NULL) { + if (outputHandle == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_HANDLE, "Handle is NULL"); - } size_t written = outputHandle->write(buffer, static_cast(length)); ret = static_cast(written); @@ -1268,16 +1291,15 @@ int cpp_audio_out_get_buffer_size(audio_out_h output, int *size) { audio_io_s* handle = static_cast(output); try { - if (handle == NULL || size == NULL) { + if (handle == NULL || size == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, size:%p", output, size); - } assert(handle->audioIoHandle); CAudioOutput* outputHandle = static_cast(handle->audioIoHandle); - if (outputHandle == NULL) { + if (outputHandle == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_HANDLE, "Handle is NULL"); - } + *size = outputHandle->getBufferSize(); } catch (CAudioError& e) { AUDIO_IO_LOGE("%s", e.getErrorMsg()); @@ -1291,10 +1313,9 @@ int cpp_audio_out_get_sample_rate(audio_out_h output, int *sample_rate) { audio_io_s* handle = static_cast(output); try { - if (handle == NULL || sample_rate == NULL) { + if (handle == NULL || sample_rate == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, sample_rate:%p", output, sample_rate); - } assert(handle->audioIoHandle); *sample_rate = handle->audioIoHandle->getAudioInfo().getSampleRate(); @@ -1310,10 +1331,9 @@ int cpp_audio_out_get_channel(audio_out_h output, audio_channel_e *channel) { audio_io_s* handle = static_cast(output); try { - if (handle == NULL || channel == NULL) { + if (handle == NULL || channel == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, channel:%p", output, channel); - } assert(handle->audioIoHandle); const CAudioInfo::EChannel srcChannel = handle->audioIoHandle->getAudioInfo().getChannel(); @@ -1333,14 +1353,13 @@ int cpp_audio_out_get_sample_type(audio_out_h output, audio_sample_type_e *type) audio_io_s* handle = static_cast(output); try { - if (handle == NULL || type == NULL) { + if (handle == NULL || type == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, type:%p", output, type); - } assert(handle->audioIoHandle); const CAudioInfo::ESampleType srcSampleType = handle->audioIoHandle->getAudioInfo().getSampleType(); - audio_sample_type_e dstSampleType = AUDIO_SAMPLE_TYPE_U8; + audio_sample_type_e dstSampleType = AUDIO_SAMPLE_TYPE_U8; __convert_audio_info_sample_type_2_sample_type(srcSampleType, dstSampleType); *type = dstSampleType; @@ -1356,14 +1375,13 @@ int cpp_audio_out_get_sound_type(audio_out_h output, sound_type_e *type) { audio_io_s* handle = static_cast(output); try { - if (handle == NULL || type == NULL) { + if (handle == NULL || type == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, type:%p", output, type); - } assert(handle->audioIoHandle); const CAudioInfo::EAudioType srcAudioType = handle->audioIoHandle->getAudioInfo().getAudioType(); - sound_type_e dstSoundType = SOUND_TYPE_MEDIA; + sound_type_e dstSoundType = SOUND_TYPE_MEDIA; __convert_audio_info_audio_type_2_sound_type(srcAudioType, dstSoundType); *type = dstSoundType; @@ -1379,17 +1397,17 @@ int cpp_audio_out_set_interrupted_cb(audio_out_h output, audio_io_interrupted_cb audio_io_s* handle = static_cast(output); try { - if (handle == NULL || callback == NULL) { + if (handle == NULL || callback == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, callback:%p", output, callback); - } assert(handle->audioIoHandle); + AUDIO_IO_LOGD("[%p], callback:[%p], user_data:[%p]", handle, callback, user_data); handle->interrupt_callback.onInterrupt = callback; handle->interrupt_callback.user_data = user_data; CAudioIO::SInterruptCallback cb = handle->audioIoHandle->getInterruptCallback(); - cb.mUserData = static_cast(handle); + cb.mUserData = static_cast(handle); cb.onInterrupt = __interrupt_cb_internal; handle->audioIoHandle->setInterruptCallback(cb); @@ -1398,6 +1416,8 @@ int cpp_audio_out_set_interrupted_cb(audio_out_h output, audio_io_interrupted_cb return __convert_CAudioError(e); } + AUDIO_IO_LOGD("[%p] done", handle); + return AUDIO_IO_ERROR_NONE; } @@ -1405,11 +1425,11 @@ int cpp_audio_out_unset_interrupted_cb(audio_out_h output) { audio_io_s* handle = static_cast(output); try { - if (handle == NULL) { + if (handle == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output); - } assert(handle->audioIoHandle); + AUDIO_IO_LOGD("[%p]", handle); handle->interrupt_callback.onInterrupt = NULL; handle->interrupt_callback.user_data = NULL; @@ -1424,6 +1444,8 @@ int cpp_audio_out_unset_interrupted_cb(audio_out_h output) { return __convert_CAudioError(e); } + AUDIO_IO_LOGD("[%p] done", handle); + return AUDIO_IO_ERROR_NONE; } @@ -1431,16 +1453,14 @@ int cpp_audio_out_ignore_session(audio_out_h output) { audio_io_s* handle = static_cast(output); try { - if (handle == NULL) { + if (handle == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output); - } - - if (handle->stream_callback.onStream) { + if (handle->stream_callback.onStream) THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "Not support ignore session in async mode"); - } assert(handle->audioIoHandle); + AUDIO_IO_LOGD("[%p]", handle); handle->audioIoHandle->ignoreSession(); } catch (CAudioError& e) { @@ -1448,6 +1468,8 @@ int cpp_audio_out_ignore_session(audio_out_h output) { return __convert_CAudioError(e); } + AUDIO_IO_LOGD("[%p] done", handle); + return AUDIO_IO_ERROR_NONE; } @@ -1455,18 +1477,18 @@ int cpp_audio_out_set_stream_cb(audio_out_h output, audio_out_stream_cb callback audio_io_s* handle = static_cast(output); try { - if (handle == NULL || callback == NULL) { + if (handle == NULL || callback == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, callback:%p", output, callback); - } assert(handle->audioIoHandle); + AUDIO_IO_LOGD("[%p], callback:[%p], user_data:[%p]", handle, callback, user_data); handle->stream_callback.onStream = callback; handle->stream_callback.user_data = user_data; CAudioIO::SStreamCallback cb = handle->audioIoHandle->getStreamCallback(); cb.mUserData = static_cast(handle); - cb.onStream = __stream_cb_internal; + cb.onStream = __stream_cb_internal; handle->audioIoHandle->setStreamCallback(cb); } catch (CAudioError& e) { @@ -1474,6 +1496,8 @@ int cpp_audio_out_set_stream_cb(audio_out_h output, audio_out_stream_cb callback return __convert_CAudioError(e); } + AUDIO_IO_LOGD("[%p] done", handle); + return AUDIO_IO_ERROR_NONE; } @@ -1481,11 +1505,11 @@ int cpp_audio_out_unset_stream_cb(audio_out_h output) { audio_io_s* handle = static_cast(output); try { - if (handle == NULL) { + if (handle == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output); - } assert(handle->audioIoHandle); + AUDIO_IO_LOGD("[%p]", handle); handle->stream_callback.onStream = NULL; handle->stream_callback.user_data = NULL; @@ -1500,6 +1524,8 @@ int cpp_audio_out_unset_stream_cb(audio_out_h output) { return __convert_CAudioError(e); } + AUDIO_IO_LOGD("[%p] done", handle); + return AUDIO_IO_ERROR_NONE; } @@ -1507,11 +1533,11 @@ int cpp_audio_out_set_state_changed_cb(audio_out_h output, audio_in_state_change audio_io_s* handle = static_cast(output); try { - if (handle == NULL || callback == NULL) { + if (handle == NULL || callback == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p, callback:%p", output, callback); - } assert(handle->audioIoHandle); + AUDIO_IO_LOGD("[%p], callback:[%p], user_data:[%p]", handle, callback, user_data); handle->state_changed_callback.onStateChanged = callback; handle->state_changed_callback.user_data = user_data; @@ -1526,6 +1552,8 @@ int cpp_audio_out_set_state_changed_cb(audio_out_h output, audio_in_state_change return __convert_CAudioError(e); } + AUDIO_IO_LOGD("[%p] done", handle); + return AUDIO_IO_ERROR_NONE; } @@ -1533,11 +1561,11 @@ int cpp_audio_out_unset_state_changed_cb(audio_out_h output) { audio_io_s* handle = static_cast(output); try { - if (handle == NULL) { + if (handle == NULL) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL output:%p", output); - } assert(handle->audioIoHandle); + AUDIO_IO_LOGD("[%p]", handle); handle->state_changed_callback.onStateChanged = NULL; handle->state_changed_callback.user_data = NULL; @@ -1552,5 +1580,7 @@ int cpp_audio_out_unset_state_changed_cb(audio_out_h output) { return __convert_CAudioError(e); } + AUDIO_IO_LOGD("[%p] done", handle); + return AUDIO_IO_ERROR_NONE; } -- 2.7.4