*/
-#include <mm.h>
-#include <pthread.h>
#include <assert.h>
-#include <audio-session-manager.h>
#include "CAudioIODef.h"
-
-#define AUDIO_IO_DEBUG
+#include <sound_manager_internal.h>
+#include <sys/time.h>
+#include <string.h>
using namespace std;
using namespace tizen_media_audio;
/**
* class CAudioIO
*/
+//LCOV_EXCL_START
CAudioIO::CAudioIO() :
- mpAudioSessionHandler(NULL),
- mpPulseAudioClient(NULL),
- __mIsInit(false),
- __mForceIgnore(false) {
+ mpPulseAudioClient(nullptr),
+ __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) : mpAudioSessionHandler(NULL), mpPulseAudioClient(NULL), __mIsInit(false), __mForceIgnore(false) {
+CAudioIO::CAudioIO(CAudioInfo& audioInfo) :
+ mpPulseAudioClient(nullptr),
+ __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;
}
-CAudioIO::~CAudioIO() {
-}
-
void CAudioIO::setInit(bool flag) {
__mIsInit = flag;
}
}
bool CAudioIO::IsReady() {
- return ((mState == CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING || mState == CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED)? true : false);
-}
-
-void CAudioIO::internalLock() throw (CAudioError) {
- if (__mIsInit == false) {
- THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
- }
-
- 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 "LOCK" COLOR_END);
-#endif
-}
-
-void CAudioIO::internalUnlock() throw (CAudioError) {
- if (__mIsInit == false) {
- THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
- }
-
- 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 "UNLOCK" COLOR_END);
-#endif
-}
-
-void CAudioIO::internalWait() throw (CAudioError) {
- 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);
-#endif
-
- pthread_cond_wait(&__mCond, &__mMutex);
-}
-
-void CAudioIO::internalSignal() throw (CAudioError) {
- 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);
-#endif
-
- pthread_cond_signal(&__mCond);
-}
-
-bool CAudioIO::isForceIgnore() {
- return __mForceIgnore;
+ return ((mState == CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING ||
+ mState == CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED));
}
-void CAudioIO::initialize() throw (CAudioError) {
- if (__mIsInit == true) {
+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()");
- }
-
- ret = pthread_cond_init(&__mCond, NULL);
- 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)
return;
- }
AUDIO_IO_LOGD("finalize");
-
- int ret = pthread_mutex_destroy(&__mMutex);
- if (ret != 0) {
- THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pthread_mutex_destroy() ret:%d", ret);
- }
-
- ret = pthread_cond_destroy(&__mCond);
- if (ret != 0) {
- THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pthread_cond_destroy() ret:%d", ret);
- }
-
__mIsInit = false;
}
void CAudioIO::onStream(CPulseAudioClient* pClient, size_t length) {
- assert(__mIsInit == true);
- assert(pClient != NULL);
+ assert(__mIsInit);
+ assert(pClient);
assert(length > 0);
#ifdef _AUDIO_IO_DEBUG_TIMING_
AUDIO_IO_LOGD("mStreamCallback.onStream(%p), pClient(%p), length(%zu)", mStreamCallback.onStream, pClient, length);
#endif
- if (mStreamCallback.onStream != NULL) {
+ if (mStreamCallback.onStream)
mStreamCallback.onStream(length, mStreamCallback.mUserData);
- }
}
void CAudioIO::onStateChanged(CAudioInfo::EAudioIOState state, bool byPolicy) {
- assert(__mIsInit == true);
+ assert(__mIsInit);
assert(state >= CAudioInfo::EAudioIOState::AUDIO_IO_STATE_NONE && state < CAudioInfo::EAudioIOState::AUDIO_IO_STATE_MAX);
- mStatePrev = mState;
- mState = state;
- mByPolicy = byPolicy;
-
- AUDIO_IO_LOGD("current(%d), previous(%d), by_policy(%d)", mState, mStatePrev, mByPolicy);
+ mByPolicy = byPolicy;
- if (mStateChangedCallback.onStateChanged != NULL) {
- mStateChangedCallback.onStateChanged(mState, mStatePrev, mByPolicy, mStateChangedCallback.mUserData);
- }
-}
+ if (mState == mStatePrev)
+ return;
-void CAudioIO::onStateChanged(CAudioInfo::EAudioIOState state) {
- onStateChanged(state, false);
-}
+ static const char* state_string[] = { "NONE", "IDLE", "RUNNING", "PAUSED" };
-void CAudioIO::onInterrupt(CAudioSessionHandler* pHandler, int id, mm_sound_focus_type_e focus_type, mm_sound_focus_state_e state, const char *reason_for_change, const char *additional_info) {
- assert(pHandler);
-
- int session_option = pHandler->getOptions();
-
- if (id == -1) {
- ///////////////////////////////////////
- // Triggered by 'focus watch callback'
- ///////////////////////////////////////
-
- if (session_option & (ASM_SESSION_OPTION_PAUSE_OTHERS | ASM_SESSION_OPTION_UNINTERRUPTIBLE)) {
- AUDIO_IO_LOGD("Session option is pausing others or uninterruptible, skip...");
- return;
- }
-
- if (state == FOCUS_IS_RELEASED) {
- // Focus handle(id) of the other application was released, do resume if possible
- internalLock();
-
- mpPulseAudioClient->cork(false);
- onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING);
-
- internalUnlock();
-
- // Focus watch callback doesn't have focus handle, but it need to convert & report to application for convenience
- state = FOCUS_IS_ACQUIRED;
- } else if (state == FOCUS_IS_ACQUIRED) {
- // Focus handle(id) of the other application was acquired, do pause if possible
- internalLock();
-
- if (mpPulseAudioClient->getStreamDirection() == CPulseAudioClient::EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
- if (mpPulseAudioClient->drain() == false) {
- AUDIO_IO_LOGE("Failed CPulseAudioClient::drain()");
- }
- }
-
- mpPulseAudioClient->cork(true);
- onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED);
-
- internalUnlock();
-
- // Focus watch callback doesn't have focus handle, but it need to convert & report to application for convenience
- state = FOCUS_IS_RELEASED;
- }
- } else {
- ///////////////////////////////////////
- // Triggered by 'focus callback'
- ///////////////////////////////////////
-
- if (pHandler->getId() != id) {
- AUDIO_IO_LOGW("Id is different, why? [mId : %d]", pHandler->getId());
- }
-
- if (session_option & ASM_SESSION_OPTION_UNINTERRUPTIBLE) {
- AUDIO_IO_LOGD("Session option is uninterruptible, skip...");
- return;
- }
-
- if (state == FOCUS_IS_RELEASED) {
- // Focus handle(id) was released, do pause here
- internalLock();
-
- if (mpPulseAudioClient->getStreamDirection() == CPulseAudioClient::EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
- if (mpPulseAudioClient->drain() == false) {
- AUDIO_IO_LOGE("Failed CPulseAudioClient::drain()");
- }
- }
-
- mpPulseAudioClient->cork(true);
- onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED);
-
- internalUnlock();
- } else if (state == FOCUS_IS_ACQUIRED) {
- // Focus handle(id) was acquired again,
- // check reason_for_change ("call-voice","call-video","voip","alarm","notification", ...)
- // do resume here and call interrupt completed callback to application.
- internalLock();
-
- mpPulseAudioClient->cork(false);
- onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING);
-
- internalUnlock();
- }
- }
+ AUDIO_IO_LOGD("previous(%s,%d) ===> current(%s,%d), by_policy(%d)",
+ state_string[static_cast<int>(mStatePrev)],
+ static_cast<int>(mStatePrev),
+ state_string[static_cast<int>(mState)],
+ static_cast<int>(mState),
+ mByPolicy);
- if (mInterruptCallback.onInterrupt != NULL) {
- IAudioSessionEventListener::EInterruptCode e = IAudioSessionEventListener::EInterruptCode::INTERRUPT_COMPLETED;
- e = IAudioSessionEventListener::convertInterruptedCode(state, reason_for_change);
- mInterruptCallback.onInterrupt(e, mInterruptCallback.mUserData);
- }
+ if (mStateChangedCallback.onStateChanged)
+ mStateChangedCallback.onStateChanged(mState, mStatePrev, mByPolicy, mStateChangedCallback.mUserData);
}
-void CAudioIO::onSignal(CAudioSessionHandler* pHandler, mm_sound_signal_name_t signal, int value) {
- assert(pHandler);
-
- if (signal == MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS) {
- if (value == 1 && pHandler->getSubscribeId() >= 0) {
- // Unregister focus watch callback & disable session handler
- pHandler->disableSessionHandler();
- AUDIO_IO_LOGD("Session handler disabled by signal");
- } else if (value == 0) {
- // Currently do nothing...
- }
- }
+void CAudioIO::setState(CAudioInfo::EAudioIOState state) {
+ mStatePrev = mState;
+ mState = state;
}
-void CAudioIO::prepare() throw (CAudioError) {
- if (__mIsInit == false) {
- THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
- }
-
- try {
- AUDIO_IO_LOGD("prepare");
- /* Do nothing */
- } catch (CAudioError e) {
- throw e;
- }
+void CAudioIO::onStateChanged(CAudioInfo::EAudioIOState state) {
+ onStateChanged(state, false);
}
-void CAudioIO::unprepare() throw (CAudioError) {
- 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 e;
- }
+CAudioInfo::EAudioIOState CAudioIO::getState() noexcept {
+ return mState;
}
-void CAudioIO::pause() throw (CAudioError) {
- if (__mIsInit == false || IsReady() == false) {
- THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioIO");
- }
+void CAudioIO::prepare() {
+ if (!__mIsInit)
+ THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); //LCOV_EXCL_LINE
+}
- try {
- internalLock();
- AUDIO_IO_LOGD("pause");
- mpPulseAudioClient->cork(true);
- internalUnlock();
- } catch (CAudioError e) {
- internalUnlock();
- throw e;
- }
+void CAudioIO::unprepare() {
+ if (!__mIsInit)
+ THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); //LCOV_EXCL_LINE
}
-void CAudioIO::resume() throw (CAudioError) {
- if (__mIsInit == false || IsReady() == false) {
- THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioIO");
- }
+void CAudioIO::pause() {
+ 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 (CAudioError e) {
- internalUnlock();
- throw e;
- }
+ AUDIO_IO_LOGD("pause");
+ mpPulseAudioClient->cork(true);
}
-void CAudioIO::drain() throw (CAudioError) {
- if (__mIsInit == false || IsReady() == false) {
- THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioIO");
- }
+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("drain");
- mpPulseAudioClient->drain();
- internalUnlock();
- } catch (CAudioError e) {
- internalUnlock();
- throw e;
- }
+ mpPulseAudioClient->cork(false);
}
-void CAudioIO::flush() throw (CAudioError) {
- if (__mIsInit == false || IsReady() == false) {
- THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioIO");
- }
+void CAudioIO::flush() {
+ 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("flush");
- mpPulseAudioClient->flush();
- internalUnlock();
- } catch (CAudioError e) {
- internalUnlock();
- throw e;
- }
+ mpPulseAudioClient->flush();
}
-CAudioInfo CAudioIO::getAudioInfo() throw (CAudioError) {
- if (__mIsInit == false) {
- THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
- }
+CAudioInfo& CAudioIO::getAudioInfo() {
+ if (!__mIsInit)
+ THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); //LCOV_EXCL_LINE
return mAudioInfo;
}
-void CAudioIO::setStreamCallback(SStreamCallback callback) throw (CAudioError) {
- if (__mIsInit == false) {
- THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
- }
+void CAudioIO::setStreamCallback(SStreamCallback callback) {
+ if (!__mIsInit)
+ THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); //LCOV_EXCL_LINE
mStreamCallback = callback;
}
-CAudioIO::SStreamCallback CAudioIO::getStreamCallback() throw (CAudioError) {
- if (__mIsInit == false) {
- THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
- }
+CAudioIO::SStreamCallback CAudioIO::getStreamCallback() {
+ if (!__mIsInit)
+ THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); //LCOV_EXCL_LINE
return mStreamCallback;
}
-void CAudioIO::setStateChangedCallback(SStateChangedCallback callback) throw (CAudioError) {
- if (__mIsInit == false) {
- THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
- }
+void CAudioIO::setStateChangedCallback(SStateChangedCallback callback) {
+ if (!__mIsInit)
+ THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); //LCOV_EXCL_LINE
mStateChangedCallback = callback;
}
-CAudioIO::SStateChangedCallback CAudioIO::getStateChangedCallback() throw (CAudioError) {
- if (__mIsInit == false) {
- THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
- }
+CAudioIO::SStateChangedCallback CAudioIO::getStateChangedCallback() {
+ if (!__mIsInit)
+ THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); //LCOV_EXCL_LINE
return mStateChangedCallback;
}
-void CAudioIO::setInterruptCallback(SInterruptCallback callback) throw (CAudioError) {
- if (__mIsInit == false) {
- THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
- }
+void CAudioIO::setStreamInfo(sound_stream_info_h stream_info) {
+ if (!stream_info)
+ THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_ARGUMENT, "stream_info is NULL"); //LCOV_EXCL_LINE
- mInterruptCallback = callback;
-}
+ if (!__mIsInit)
+ THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO"); //LCOV_EXCL_LINE
-CAudioIO::SInterruptCallback CAudioIO::getInterruptCallback() throw (CAudioError) {
- if (__mIsInit == false) {
- THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
- }
+ if (mState != CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE)
+ THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_STATE, "it is not permitted while started"); //LCOV_EXCL_LINE
- return mInterruptCallback;
-}
+ int errorCode = SOUND_MANAGER_ERROR_NONE;
+ char *type = nullptr;
+ int index = -1;
+ bool avail = false;
+ 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); //LCOV_EXCL_LINE
+ if (!avail)
+ THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE, "Input stream is not supported"); //LCOV_EXCL_LINE
-void CAudioIO::ignoreSession() throw (CAudioError) {
- if (__mIsInit == false) {
- THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Doesn't initialize CAudioIO");
- }
+ 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); //LCOV_EXCL_LINE
+ if (mDirection == CAudioInfo::EAudioDirection::AUDIO_DIRECTION_IN)
+ getAudioInfo().setAudioTypeByInputStreamType(type);
+ else
+ getAudioInfo().setAudioTypeByOutputStreamType(type);
- try {
- internalLock();
+ 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 (mpPulseAudioClient != NULL && mpPulseAudioClient->isCorked() == false) {
- THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "An Operation is not permitted while started");
- }
+ if (mDirection == CAudioInfo::EAudioDirection::AUDIO_DIRECTION_IN) {
+ int device_id;
- bool isSkip = mpAudioSessionHandler->isSkipSessionEvent();
- if (isSkip == false && mpAudioSessionHandler->getId() >= 0) {
- mpAudioSessionHandler->unregisterSound();
- __mForceIgnore = true;
- }
+ if ((errorCode = sound_manager_get_echo_cancel_reference_device(stream_info, &device_id) != SOUND_MANAGER_ERROR_NONE))
+ THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Can't get reference device [ret:%d]", errorCode); //LCOV_EXCL_LINE
- internalUnlock();
- } catch (CAudioError e) {
- internalUnlock();
- throw e;
+ if (device_id != SOUND_MANAGER_STREAM_NO_REFERENCE_DEVICE)
+ getAudioInfo().bindEchoCancelReferenceDeviceId(device_id);
}
}