X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=src%2Fcpp%2FCAudioInput.cpp;h=d930137edd3fb9594256680ea48b2d3c606196d4;hb=1451d425ee207a9ca88a04e5dfef1e7d9c2e3340;hp=903ee7d8099cc2fd7e3bcb2207d4f8a3f9fd47e9;hpb=6f049e4e45d92d8d248c74d1ff6a1b175c16414f;p=platform%2Fcore%2Fapi%2Faudio-io.git diff --git a/src/cpp/CAudioInput.cpp b/src/cpp/CAudioInput.cpp index 903ee7d..d930137 100644 --- a/src/cpp/CAudioInput.cpp +++ b/src/cpp/CAudioInput.cpp @@ -17,9 +17,9 @@ #include -#include #include "CAudioIODef.h" #include +#include "cpp_audio_in_privilege.h" #define RECORDER_PRIVILEGE "http://tizen.org/privilege/recorder" #define CLIENT_NAME "AUDIO_IO_PA_CLIENT" @@ -27,11 +27,6 @@ using namespace std; using namespace tizen_media_audio; -struct PrivilegeData { - bool isPrivilegeAllowed; - pa_threaded_mainloop *paMainloop; -}; - /** * class CAudioInput inherited by CAudioIO */ @@ -42,20 +37,6 @@ CAudioInput::CAudioInput(CAudioInfo& info) : mDirection = CAudioInfo::EAudioDirection::AUDIO_DIRECTION_IN; } -CAudioInput::CAudioInput( - unsigned int sampleRate, - CAudioInfo::EChannel channel, - CAudioInfo::ESampleType type, - CAudioInfo::EAudioType audioType) : - __mIsUsedSyncRead(true), - __mIsInit(false) { - mDirection = CAudioInfo::EAudioDirection::AUDIO_DIRECTION_IN; - mAudioInfo = CAudioInfo(sampleRate, channel, type, audioType, -1); -} - -CAudioInput::~CAudioInput() { -} - void CAudioInput::onStream(CPulseAudioClient* pClient, size_t length) { assert(pClient); @@ -63,9 +44,9 @@ void CAudioInput::onStream(CPulseAudioClient* pClient, size_t length) { * Does not call CAudioIO::onStream() for synchronization * if a user is using read() */ - if (__mIsUsedSyncRead == true) { + if (__mIsUsedSyncRead) { #ifdef _AUDIO_IO_DEBUG_TIMING_ - AUDIO_IO_LOGD("Sync Read Mode! - pClient:[%p], length:[%d]", pClient, length); + AUDIO_IO_LOGD("Sync Read Mode! - pClient:[%p], length:[%zu]", pClient, length); #endif return; } @@ -74,191 +55,57 @@ void CAudioInput::onStream(CPulseAudioClient* pClient, size_t length) { * Accrues callback function */ #ifdef _AUDIO_IO_DEBUG_TIMING_ - AUDIO_IO_LOGD("pClient:[%p], length:[%d]", pClient, length); + AUDIO_IO_LOGD("pClient:[%p], length:[%zu]", pClient, length); #endif CAudioIO::onStream(pClient, length); } -void CAudioInput::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); - 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); -} - -void CAudioInput::__setInit(bool flag) { +void CAudioInput::__setInit(bool flag) noexcept { __mIsInit = flag; } -bool CAudioInput::__IsInit() { - return (CAudioIO::isInit() == true && __mIsInit == true); +bool CAudioInput::__IsInit() noexcept { + return (CAudioIO::isInit() && __mIsInit); } -bool CAudioInput::__IsReady() { +bool CAudioInput::__IsReady() noexcept { return CAudioIO::IsReady(); } -static void __contextStateChangeCb(pa_context* c, void* user_data) { - pa_threaded_mainloop *paMainloop = static_cast(user_data); - assert(paMainloop); - assert(c); - - switch (pa_context_get_state(c)) { - case PA_CONTEXT_READY: - AUDIO_IO_LOGD("The context is ready"); - pa_threaded_mainloop_signal(paMainloop, 0); - break; - - case PA_CONTEXT_FAILED: - case PA_CONTEXT_TERMINATED: - AUDIO_IO_LOGD("The context is lost"); - pa_threaded_mainloop_signal(paMainloop, 0); - break; - - case PA_CONTEXT_UNCONNECTED: - case PA_CONTEXT_CONNECTING: - case PA_CONTEXT_AUTHORIZING: - case PA_CONTEXT_SETTING_NAME: - break; - } -} - -static void __checkPrivilegeCb(pa_context *c, int success, void *user_data) { - AUDIO_IO_LOGD("pa_context[%p], success[%d], user_data[%p]", c, success, user_data); - assert(c); - assert(user_data); - - PrivilegeData *prData = static_cast(user_data); - prData->isPrivilegeAllowed = success ? true : false; - - pa_threaded_mainloop_signal(prData->paMainloop, 0); -} - -static bool __IsPrivilegeAllowed() { - pa_operation *o; - pa_context *c; - int err = 0; - PrivilegeData prData; - - 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()"); - - pa_context_set_state_callback(c, __contextStateChangeCb, prData.paMainloop); - - if (pa_context_connect(c, NULL, PA_CONTEXT_NOFLAGS, NULL) < 0) - THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_context_connect()"); - - pa_threaded_mainloop_lock(prData.paMainloop); - - if (pa_threaded_mainloop_start(prData.paMainloop) < 0) { - pa_threaded_mainloop_unlock(prData.paMainloop); - THROW_ERROR_MSG(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_threaded_mainloop_start()"); - } - - while (true) { - pa_context_state_t state; - state = pa_context_get_state(c); - - if (state == PA_CONTEXT_READY) - break; - - if (!PA_CONTEXT_IS_GOOD(state)) { - err = pa_context_errno(c); - pa_threaded_mainloop_unlock(prData.paMainloop); - THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, - "pa_context's state is not good : err[%d]", err); - } - - /* Wait until the context is ready */ - pa_threaded_mainloop_wait(prData.paMainloop); - } - - o = pa_context_check_privilege(c, RECORDER_PRIVILEGE, __checkPrivilegeCb, &prData); - if (!o) { - pa_threaded_mainloop_unlock(prData.paMainloop); - THROW_ERROR_MSG(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed to pa_context_check_privilege()"); - } - while (pa_operation_get_state(o) == PA_OPERATION_RUNNING) - pa_threaded_mainloop_wait(prData.paMainloop); - pa_operation_unref(o); - - pa_threaded_mainloop_unlock(prData.paMainloop); - pa_threaded_mainloop_stop(prData.paMainloop); - pa_context_disconnect(c); - pa_context_unref(c); - pa_threaded_mainloop_free(prData.paMainloop); - - return prData.isPrivilegeAllowed; -} - void CAudioInput::initialize() { - if (__IsInit() == true) + if (__IsInit()) return; - try { - CAudioIO::initialize(); - } catch (CAudioError& e) { - finalize(); - throw; - } - - if (__IsPrivilegeAllowed() == false) + if (!cpp_audio_in_has_record_privilege()) THROW_ERROR_MSG(CAudioError::EError::ERROR_PERMISSION_DENIED, "No privilege for record"); try { - internalSessionLock(); - mpAudioSessionHandler = new CAudioSessionHandler(CAudioSessionHandler::EAudioSessionType::AUDIO_SESSION_TYPE_CAPTURE, mAudioInfo, this); - mpAudioSessionHandler->initialize(); - internalSessionUnlock(); - } catch (CAudioError& e) { - internalSessionUnlock(); + CAudioIO::initialize(); + __setInit(true); + } catch (const CAudioError& e) { 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() { - if (__IsInit() == false) { + if (!__IsInit()) { AUDIO_IO_LOGD("Did not initialize"); return; } - internalSessionLock(); - SAFE_FINALIZE(mpAudioSessionHandler); - SAFE_DELETE(mpAudioSessionHandler); - internalSessionUnlock(); - CAudioIO::finalize(); __setInit(false); } void CAudioInput::prepare() { - if (__IsInit() == false) + if (!__IsInit()) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CAudioInput"); - if (__IsReady() == true) { + if (__IsReady()) { AUDIO_IO_LOGD("Already prepared CAudioInput"); CAudioIO::prepare(); return; @@ -272,37 +119,13 @@ void CAudioInput::prepare() { "The audioType is invalid [type:%d]", static_cast(audioType)); try { - 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(); - - try { - internalSessionLock(); - if (isSessionEnabled() && isForceIgnore() == false) - mpAudioSessionHandler->updatePlaying(); - internalSessionUnlock(); - } catch (CAudioError& e) { - internalSessionUnlock(); - throw; - } - - try { /* Init StreamSpec */ AUDIO_IO_LOGD("Set Stream Spec : CPulseStreamSpec::STREAM_LATENCY_INPUT_DEFAULT"); CPulseStreamSpec::EStreamLatency streamSpec = CPulseStreamSpec::EStreamLatency::STREAM_LATENCY_INPUT_DEFAULT; + /* Override the default value by audio type */ + if (audioType == CAudioInfo::EAudioType::AUDIO_IN_TYPE_VOIP) + streamSpec = CPulseStreamSpec::EStreamLatency::STREAM_LATENCY_INPUT_VOIP; + CPulseStreamSpec spec(streamSpec, mAudioInfo); internalLock(); @@ -315,23 +138,25 @@ void CAudioInput::prepare() { internalUnlock(); CAudioIO::prepare(); - } catch (CAudioError& e) { + } catch (const CAudioError& e) { SAFE_FINALIZE(mpPulseAudioClient); SAFE_DELETE(mpPulseAudioClient); internalUnlock(); throw; } catch (const std::bad_alloc&) { +//LCOV_EXCL_START internalUnlock(); THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed to allocate CPulseAudioClient object"); +//LCOV_EXCL_STOP } } void CAudioInput::unprepare() { - if (__IsInit() == false) + if (!__IsInit()) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CAudioInput"); - if (__IsReady() == false) { + if (!__IsReady()) { AUDIO_IO_LOGD("Already unprepared"); return; } @@ -345,26 +170,16 @@ void CAudioInput::unprepare() { SAFE_FINALIZE(mpPulseAudioClient); SAFE_DELETE(mpPulseAudioClient); internalUnlock(); - } catch (CAudioError& e) { + } catch (const CAudioError& e) { internalUnlock(); throw; } - try { - internalSessionLock(); - if (isSessionEnabled() && isForceIgnore() == false) - mpAudioSessionHandler->updateStop(); - internalSessionUnlock(); - - CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE); - } catch (CAudioError& e) { - internalSessionUnlock(); - throw; - } + CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE); } void CAudioInput::pause() { - if (__IsInit() == false || __IsReady() == false) + if (!__IsInit() || !__IsReady()) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioInput"); @@ -372,7 +187,7 @@ void CAudioInput::pause() { THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_STATE, "Can't pause if not in Running state"); - if (mpPulseAudioClient->isInThread() == true) + if (mpPulseAudioClient->isInThread()) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_OPERATION, "Can't pause in thread"); CAudioIO::pause(); @@ -380,7 +195,7 @@ void CAudioInput::pause() { } void CAudioInput::resume() { - if (__IsInit() == false || __IsReady() == false) + if (!__IsInit() || !__IsReady()) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioInput"); @@ -388,19 +203,15 @@ void CAudioInput::resume() { THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_STATE, "Can't resume if not in Paused state"); - if (mpPulseAudioClient->isInThread() == true) + if (mpPulseAudioClient->isInThread()) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_OPERATION, "Can't resume in thread"); CAudioIO::resume(); CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING); } -void CAudioInput::drain() { - THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "Did not support drain of CAudioInput"); -} - void CAudioInput::flush() { - if (__IsInit() == false || __IsReady() == false) + if (!__IsInit() || !__IsReady()) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioInput"); @@ -408,7 +219,7 @@ void CAudioInput::flush() { } int CAudioInput::getBufferSize() { - if (__IsInit() == false) + if (!__IsInit()) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CAudioInput"); /* FIXME : return calculated size here to satisfy backward compatibility */ @@ -416,24 +227,22 @@ int CAudioInput::getBufferSize() { } void CAudioInput::setStreamCallback(SStreamCallback callback) { - if (__IsInit() == false) + if (!__IsInit()) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CAudioInput"); - if (callback.onStream == NULL) - __mIsUsedSyncRead = true; - else - __mIsUsedSyncRead = false; + __mIsUsedSyncRead = (callback.onStream == nullptr); + 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() || !__IsReady()) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioInput"); - if (buffer == NULL) + if (!buffer) THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are NULL buffer:%p", buffer); @@ -442,7 +251,7 @@ size_t CAudioInput::read(void* buffer, size_t length) { "Can't read if not in Running state"); /* Checks synchronous flag */ - if (__mIsUsedSyncRead == false) + if (!__mIsUsedSyncRead) THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "Invalid operation of read() if receive stream callback"); @@ -450,11 +259,9 @@ size_t CAudioInput::read(void* buffer, size_t length) { try { internalLock(); - 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) + if (!mpPulseAudioClient) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient"); @@ -463,7 +270,7 @@ size_t CAudioInput::read(void* buffer, size_t length) { internalUnlock(); sched_yield(); - } catch (CAudioError& e) { + } catch (const CAudioError& e) { internalUnlock(); throw; } @@ -472,16 +279,16 @@ 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() || !__IsReady()) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioInput"); - if (buffer == NULL || length == NULL) + if (buffer == nullptr || length == nullptr) 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) THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "Invalid operation of peek() if does not receive a stream callback"); @@ -489,12 +296,12 @@ int CAudioInput::peek(const void** buffer, size_t* length) { } int CAudioInput::drop() { - if (__IsInit() == false || __IsReady() == false) + if (!__IsInit() || !__IsReady()) THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioInput"); /* Checks synchronous flag */ - if (__mIsUsedSyncRead == true) + if (__mIsUsedSyncRead) THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "Invalid operation of drop() if does not receive a stream callback");