#include <new>
-#include <pulse/pulseaudio.h>
#include "CAudioIODef.h"
#include <sched.h>
+#include "cpp_audio_in_privilege.h"
#define RECORDER_PRIVILEGE "http://tizen.org/privilege/recorder"
#define CLIENT_NAME "AUDIO_IO_PA_CLIENT"
using namespace std;
using namespace tizen_media_audio;
-struct PrivilegeData {
- bool isPrivilegeAllowed;
- pa_threaded_mainloop *paMainloop;
-};
-
/**
* class CAudioInput inherited by CAudioIO
*/
mDirection = CAudioInfo::EAudioDirection::AUDIO_DIRECTION_IN;
}
+//LCOV_EXCL_START
CAudioInput::CAudioInput(
unsigned int sampleRate,
CAudioInfo::EChannel channel,
mDirection = CAudioInfo::EAudioDirection::AUDIO_DIRECTION_IN;
mAudioInfo = CAudioInfo(sampleRate, channel, type, audioType, -1);
}
+//LCOV_EXCL_STOP
CAudioInput::~CAudioInput() {
}
*/
if (__mIsUsedSyncRead == true) {
#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;
}
* 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) {
__mIsInit = flag;
}
return CAudioIO::IsReady();
}
-static void __contextStateChangeCb(pa_context* c, void* user_data) {
- pa_threaded_mainloop *paMainloop = static_cast<pa_threaded_mainloop*>(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<PrivilegeData*>(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() == true)
return;
- }
+
+ if (cpp_audio_in_has_record_privilege() == false)
+ THROW_ERROR_MSG(CAudioError::EError::ERROR_PERMISSION_DENIED, "No privilege for record");
try {
CAudioIO::initialize();
- if (__IsPrivilegeAllowed() == false) {
- THROW_ERROR_MSG(CAudioError::EError::ERROR_PERMISSION_DENIED, "No privilege for record");
- }
-
- // Create ASM Handler
- mpAudioSessionHandler = new CAudioSessionHandler(CAudioSessionHandler::EAudioSessionType::AUDIO_SESSION_TYPE_CAPTURE, mAudioInfo, this);
-
- // Initialize ASM Handler
- mpAudioSessionHandler->initialize();
-
__setInit(true);
- CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE);
} catch (CAudioError& e) {
finalize();
throw;
- } catch (const std::bad_alloc&) {
- finalize();
- THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed to allocate CAudioSessionHandler object");
}
+
+ CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE);
}
void CAudioInput::finalize() {
return;
}
- SAFE_FINALIZE(mpAudioSessionHandler);
- SAFE_DELETE(mpAudioSessionHandler);
-
CAudioIO::finalize();
__setInit(false);
}
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");
return;
}
- try {
- internalLock();
+ /* Check invalid AudioType */
+ CAudioInfo::EAudioType audioType = mAudioInfo.getAudioType();
+ 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<int>(audioType));
- // Check to invalid AudioType
- CAudioInfo::EAudioType audioType = mAudioInfo.getAudioType();
- 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<int>(audioType));
- }
-
- if (mpAudioSessionHandler->getId() < 0) { // Did not registerSound()
- // Check session to skip registration
- if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSession() == false) {
- // Register ASM Listener
- AUDIO_IO_LOGD("Register ASM Listener");
- mpAudioSessionHandler->registerSound();
- }
- }
-
- CAudioIO::setInternalStreamInfo();
-
- // Init StreamSpec
+ 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);
- // Create PulseAudio Handler
+ internalLock();
mpPulseAudioClient = new CPulseAudioClient(CPulseAudioClient::EStreamDirection::STREAM_DIRECTION_RECORD, spec, this);
-
- // Initialize PulseAudio Handler
mpPulseAudioClient->initialize();
-
- if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSession() == false)
- mpAudioSessionHandler->updatePlaying();
-
#ifndef DISABLE_MOBILE_BACK_COMP
- // Uncork stream which is created with CORKED flag */
+ /* Uncork stream which is created with CORKED flag */
mpPulseAudioClient->cork(false);
#endif
-
internalUnlock();
- // Do Prepare
CAudioIO::prepare();
} catch (CAudioError& e) {
SAFE_FINALIZE(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() == 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();
-
+ if (mpPulseAudioClient && mpPulseAudioClient->isInThread())
+ THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "Can't unprepare inside pulseaudio thread");
SAFE_FINALIZE(mpPulseAudioClient);
SAFE_DELETE(mpPulseAudioClient);
-
internalUnlock();
-
- if (mpAudioSessionHandler->getId() >= 0) {
- if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSession() == false)
- mpAudioSessionHandler->updateStop();
- }
-
- CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE);
} catch (CAudioError& e) {
internalUnlock();
throw;
}
+
+ CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE);
}
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();
-
- internalLock();
- /* Updates ASM to STOP */
- if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSession() == false) {
- mpAudioSessionHandler->updateStop();
- }
-
- internalUnlock();
-
- CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED);
- } catch (CAudioError& e) {
- internalUnlock();
- 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 {
- internalLock();
-
- if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSession() == false)
- mpAudioSessionHandler->updatePlaying();
-
- internalUnlock();
-
- CAudioIO::resume();
- CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING);
- } catch (CAudioError& e) {
- internalUnlock();
- throw;
- }
-}
-
-void CAudioInput::drain() {
- THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "Did not support drain of CAudioInput");
+ CAudioIO::resume();
+ CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING);
}
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) {
- THROW_ERROR_MSG(CAudioError::EError::ERROR_POLICY_BLOCKED, "audio-io is interrupted");
- }
// If another thread did call unprepare, do not read
if (mpPulseAudioClient == NULL)
// Block until read done
ret = mpPulseAudioClient->read(buffer, length);
-
internalUnlock();
+
sched_yield();
} catch (CAudioError& e) {
internalUnlock();
}
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();
}