Add pa_threaded_mainloop_lock() in CPulseAudioClient::write()
[platform/core/api/audio-io.git] / src / cpp / CPulseAudioClient.cpp
index 55d2f39..7f41a61 100644 (file)
  */
 
 
-#include <mm.h>
 #include "CAudioIODef.h"
-
+#include <unistd.h>
+#include <inttypes.h>
+#ifdef ENABLE_DPM
+#include <dpm/restriction.h>
+#endif
 
 using namespace std;
 using namespace tizen_media_audio;
@@ -27,30 +30,50 @@ using namespace tizen_media_audio;
  * class CPulseAudioClient
  */
 const char* CPulseAudioClient::CLIENT_NAME = "AUDIO_IO_PA_CLIENT";
-
-CPulseAudioClient::CPulseAudioClient(EStreamDirection      direction,
-                                     CPulseStreamSpec&     spec,
-                                     IPulseStreamListener* listener)
-    : mDirection(direction), mSpec(spec),     mpListener(listener),
-      mpMainloop(NULL),      mpContext(NULL), mpStream(NULL),
-      mpPropList(NULL),      mIsInit(false),  mIsOperationSuccess(false) {
+static const unsigned int drain_wait_interval = 10000;
+static const unsigned int drain_wait_max_count = 30;
+
+CPulseAudioClient::CPulseAudioClient(
+        EStreamDirection      direction,
+        CPulseStreamSpec&     spec,
+        IPulseStreamListener* listener) :
+    __mDirection(direction),
+    __mSpec(spec),
+    __mpListener(listener),
+    __mpMainloop(NULL),
+    __mpContext(NULL),
+    __mpStream(NULL),
+    __mpPropList(NULL),
+    __mIsInit(false),
+    __mIsOperationSuccess(false),
+    __mpSyncReadDataPtr(NULL),
+    __mSyncReadIndex(0),
+    __mSyncReadLength(0),
+    __mIsUsedSyncRead(false),
+    __mIsFirstStream(false),
+    __mIsDraining(false),
+    __mIsStarted(false) {
 }
 
 CPulseAudioClient::~CPulseAudioClient() {
     finalize();
 }
 
-void CPulseAudioClient::_contextStateChangeCb(pa_context* c, void* user_data) {
+void CPulseAudioClient::__contextStateChangeCb(pa_context* c, void* user_data) {
     CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
     assert(pClient);
     assert(c);
 
     switch (pa_context_get_state(c)) {
     case PA_CONTEXT_READY:
-        AUDIO_IO_LOGD("The context is ready!");
+        AUDIO_IO_LOGD("The context is ready");
+        pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
+        break;
+
     case PA_CONTEXT_FAILED:
     case PA_CONTEXT_TERMINATED:
-        pa_threaded_mainloop_signal(pClient->mpMainloop, 0);
+        AUDIO_IO_LOGD("The context is lost");
+        pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
         break;
 
     case PA_CONTEXT_UNCONNECTED:
@@ -61,18 +84,37 @@ void CPulseAudioClient::_contextStateChangeCb(pa_context* c, void* user_data) {
     }
 }
 
-void CPulseAudioClient::_successContextCb(pa_context* c, int success, void* user_data) {
+void CPulseAudioClient::__successContextCb(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);
 
     CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
-    pClient->mIsOperationSuccess = static_cast<bool>(success);
+    pClient->__mIsOperationSuccess = static_cast<bool>(success);
 
-    pa_threaded_mainloop_signal(pClient->mpMainloop, 0);
+    pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
 }
 
-void CPulseAudioClient::_streamStateChangeCb(pa_stream* s, void* user_data) {
+static bool __is_microphone_restricted(void) {
+    int state = 1;
+#ifdef ENABLE_DPM
+    device_policy_manager_h dpm_h = NULL;
+    int ret = 0;
+
+    if ((dpm_h = dpm_manager_create())) {
+        /* state: 0(disallowed), 1(allowed) */
+        if ((ret = dpm_restriction_get_microphone_state(dpm_h, &state)))
+            AUDIO_IO_LOGE("Failed to dpm_restriction_get_microphone_state(), ret(0x%x)", ret);
+        dpm_manager_destroy(dpm_h);
+        AUDIO_IO_LOGD("microphone restriction state: %d(1:allowed, 0:disallowed)", state);
+    } else {
+        AUDIO_IO_LOGE("Failed to dpm_manager_create()");
+    }
+#endif
+    return (state ? false : true);
+}
+
+void CPulseAudioClient::__streamStateChangeCb(pa_stream* s, void* user_data) {
     assert(s);
     assert(user_data);
 
@@ -80,75 +122,170 @@ void CPulseAudioClient::_streamStateChangeCb(pa_stream* s, void* user_data) {
 
     switch (pa_stream_get_state(s)) {
     case PA_STREAM_READY:
-        AUDIO_IO_LOGD("The stream is ready!");
-        pClient->mpListener->onStateChanged(CAudioInfo::AUDIO_IO_STATE_RUNNING);
+        AUDIO_IO_LOGD("The stream is ready");
+        pClient->__mpListener->onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING);
+        if (pClient->__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK)
+            pClient->__mIsFirstStream = true;
+        pClient->__mIsInit = true;
+        pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
+        break;
+
     case PA_STREAM_FAILED:
+        AUDIO_IO_LOGD("The stream is failed");
+        pClient->__mpListener->onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE,
+                                              __is_microphone_restricted());
+        pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
+        break;
+
     case PA_STREAM_TERMINATED:
-        pa_threaded_mainloop_signal(pClient->mpMainloop, 0);
+        AUDIO_IO_LOGD("The stream is terminated");
+        pClient->__mpListener->onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE);
+        pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
         break;
 
     case PA_STREAM_UNCONNECTED:
-        break;
     case PA_STREAM_CREATING:
         break;
     }
 }
 
-void CPulseAudioClient::_streamCaptureCb(pa_stream* s, size_t length, void* user_data) {
+void CPulseAudioClient::__streamCaptureCb(pa_stream* s, size_t length, void* user_data) {
     assert(s);
     assert(user_data);
 
     CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
-    assert(pClient->mpListener);
+    assert(pClient->__mpListener);
+    assert(pClient->__mpMainloop);
+
+    if (pClient->__mIsUsedSyncRead == true) {
+        pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
+    }
 
-    pClient->mpListener->onStream(pClient, length);
+    pClient->__mpListener->onStream(pClient, length);
 }
 
-void CPulseAudioClient::_streamPlaybackCb(pa_stream* s, size_t length, void* user_data) {
-    //AUDIO_IO_LOGD("_streamPlaybackCb()");
+#ifndef DISABLE_MOBILE_BACK_COMP
+static void __dummy_write(pa_stream* s, size_t length) {
+    char* dummy = new char[length];
+    memset(dummy, 0, length);
+    pa_stream_write(s, dummy, length, NULL, 0LL, PA_SEEK_RELATIVE);
+    delete [] dummy;
+}
+#endif
+
+void CPulseAudioClient::__streamPlaybackCb(pa_stream* s, size_t length, void* user_data) {
     assert(s);
     assert(user_data);
 
     CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
-    assert(pClient->mpListener);
+    assert(pClient->__mpListener);
 
-    if (pClient->mIsInit == false) {
-        AUDIO_IO_LOGD("Occurred this listener when an out stream is on the way to create - Dummy write[length:%d]", length);
+#ifndef DISABLE_MOBILE_BACK_COMP
+    if (pClient->__mIsInit == false) {
+        AUDIO_IO_LOGD("Occurred this listener when an out stream is on the way to create : Write dummy, length[%d]", length);
+        __dummy_write(s, length);
+        return;
+    }
+    if (pClient->isCorked()) {
+        AUDIO_IO_LOGD("Occurred this listener when an out stream is CORKED : Write dummy, length[%d]", length);
+        __dummy_write(s, length);
+        return;
+    }
+#endif
 
-        char* dummy = new char[length];
-        memset(dummy, 0, length);
-        pa_stream_write(s, dummy, length, NULL, 0LL, PA_SEEK_RELATIVE);
-        delete [] dummy;
+    pClient->__mpListener->onStream(pClient, length);
 
-        return;
+#ifndef DISABLE_MOBILE_BACK_COMP
+    /* If stream is not written in first callback during prepare,
+       then write dummy data to ensure the start */
+    if (pClient->__mSpec.getStreamLatency() == CPulseStreamSpec::EStreamLatency::STREAM_LATENCY_OUTPUT_DEFAULT_ASYNC &&
+        pClient->__mIsFirstStream) {
+        AUDIO_IO_LOGW("[async] Write dummy of length[%d] since not written in first callback during prepare", length);
+        __dummy_write(s, length);
+        pClient->__mIsFirstStream = false;
     }
+#endif
+}
+
+void CPulseAudioClient::__streamLatencyUpdateCb(pa_stream* s, void* user_data) {
+    assert(s);
+    assert(user_data);
 
-    pClient->mpListener->onStream(pClient, length);
+    CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
+
+    pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
 }
 
-void CPulseAudioClient::_streamLatencyUpdateCb(pa_stream* s, void* user_data) {
+void CPulseAudioClient::__streamStartedCb(pa_stream* s, void* user_data) {
     assert(s);
     assert(user_data);
 
     CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
 
-    pa_threaded_mainloop_signal(pClient->mpMainloop, 0);
+    AUDIO_IO_LOGD("stream %p started.", pClient);
+
+    pClient->__mIsStarted = true;
 }
 
-void CPulseAudioClient::_successStreamCb(pa_stream* s, int success, void* user_data) {
+void CPulseAudioClient::__streamUnderflowCb(pa_stream* s, void* user_data) {
+    assert(s);
+    assert(user_data);
+
+    CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
+
+    AUDIO_IO_LOGD("stream %p UnderFlow...", pClient);
+
+    pClient->__mIsStarted = false;
+}
+
+void CPulseAudioClient::__streamEventCb(pa_stream* s, const char *name, pa_proplist *pl, void *user_data) {
+    assert(s);
+    assert(user_data);
+
+    AUDIO_IO_LOGE("NAME : %s, Prop : %p", name, pl);
+
+    CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
+    if (strcmp(name, PA_STREAM_EVENT_POP_TIMEOUT) == 0) {
+        pa_operation_unref(pa_stream_cork(pClient->__mpStream, 1, NULL, NULL));
+    }
+}
+
+
+void CPulseAudioClient::__successStreamCb(pa_stream* s, int success, void* user_data) {
     AUDIO_IO_LOGD("pa_stream[%p], success[%d], user_data[%p]", s, success, user_data);
     assert(s);
     assert(user_data);
 
     CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
-    pClient->mIsOperationSuccess = static_cast<bool>(success);
+    pClient->__mIsOperationSuccess = static_cast<bool>(success);
 
-    pa_threaded_mainloop_signal(pClient->mpMainloop, 0);
+    pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
 }
 
-void CPulseAudioClient::initialize() throw (CAudioError) {
-    AUDIO_IO_LOGD("");
-    if (mIsInit == true) {
+void CPulseAudioClient::__successDrainCbInThread(pa_stream* s, int success, void* user_data) {
+    AUDIO_IO_LOGD("pa_stream[%p], success[%d], user_data[%p]", s, success, user_data);
+    assert(s);
+    assert(user_data);
+
+    CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
+    pClient->__mIsOperationSuccess = static_cast<bool>(success);
+    pClient->__mIsDraining = false;
+}
+
+void CPulseAudioClient::__successDrainCb(pa_stream* s, int success, void* user_data) {
+    AUDIO_IO_LOGD("pa_stream[%p], success[%d], user_data[%p]", s, success, user_data);
+    assert(s);
+    assert(user_data);
+
+    CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
+    pClient->__mIsOperationSuccess = static_cast<bool>(success);
+    pClient->__mIsDraining = false;
+
+    pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
+}
+
+void CPulseAudioClient::initialize() {
+    if (__mIsInit == true) {
         return;
     }
 
@@ -157,55 +294,53 @@ void CPulseAudioClient::initialize() throw (CAudioError) {
 
     try {
         // Allocates PA proplist
-        mpPropList = pa_proplist_new();
-        if (mpPropList == NULL) {
-            THROW_ERROR_MSG(CAudioError::ERROR_OUT_OF_MEMORY, "Failed pa_proplist_new()");
+        __mpPropList = pa_proplist_new();
+        if (__mpPropList == NULL) {
+            THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_proplist_new()");
         }
 
         // Adds values on proplist for delivery to PULSEAUDIO
         char *streamType = NULL;
-        CAudioInfo::EAudioType audioType = mSpec.getAudioInfo().getAudioType();
-        mSpec.getAudioInfo().convertAudioType2StreamType(audioType, &streamType);
-        pa_proplist_sets(mpPropList, PA_PROP_MEDIA_ROLE, streamType);
+        CAudioInfo::EAudioType audioType = __mSpec.getAudioInfo().getAudioType();
+        __mSpec.getAudioInfo().convertAudioType2StreamType(audioType, &streamType);
+        pa_proplist_sets(__mpPropList, PA_PROP_MEDIA_ROLE, streamType);
 
-        int index = mSpec.getAudioInfo().getAudioIndex();
+        int index = __mSpec.getAudioInfo().getAudioIndex();
         if (index >= 0) {
-            pa_proplist_setf(mpPropList, PA_PROP_MEDIA_PARENT_ID, "%u", (unsigned int) index);
+            pa_proplist_setf(__mpPropList, PA_PROP_MEDIA_PARENT_ID, "%u", (unsigned int) index);
         }
 
         // Adds latency on proplist for delivery to PULSEAUDIO
-        CPulseStreamSpec::EStreamLatency latency = mSpec.getStreamLatency();
-        AUDIO_IO_LOGD("LATENCY : %d", latency);
-
-        pa_proplist_setf(mpPropList, PA_PROP_MEDIA_TIZEN_AUDIO_LATENCY, "%d", latency);
+        AUDIO_IO_LOGD("LATENCY : %s[%d]", __mSpec.getStreamLatencyToString(), static_cast<int>(__mSpec.getStreamLatency()));
+        pa_proplist_setf(__mpPropList, PA_PROP_MEDIA_TIZEN_AUDIO_LATENCY, "%s", __mSpec.getStreamLatencyToString());
 
         // Allocates PA mainloop
-        mpMainloop = pa_threaded_mainloop_new();
-        if (mpMainloop == NULL) {
-            THROW_ERROR_MSG(CAudioError::ERROR_OUT_OF_MEMORY, "Failed pa_threaded_mainloop_new()");
+        __mpMainloop = pa_threaded_mainloop_new();
+        if (__mpMainloop == NULL) {
+            THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_threaded_mainloop_new()");
         }
 
         // Allocates PA context
-        mpContext = pa_context_new(pa_threaded_mainloop_get_api(mpMainloop), CLIENT_NAME);
-        if (mpContext == NULL) {
-            THROW_ERROR_MSG(CAudioError::ERROR_OUT_OF_MEMORY, "Failed pa_context_new()");
+        __mpContext = pa_context_new(pa_threaded_mainloop_get_api(__mpMainloop), CLIENT_NAME);
+        if (__mpContext == NULL) {
+            THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_context_new()");
         }
 
         // Sets context state changed callback
-        pa_context_set_state_callback(mpContext, _contextStateChangeCb, this);
+        pa_context_set_state_callback(__mpContext, __contextStateChangeCb, this);
 
         // Connects this client with PA server
-        if (pa_context_connect(mpContext, NULL, PA_CONTEXT_NOFLAGS, NULL) < 0) {
-            THROW_ERROR_MSG(CAudioError::ERROR_OUT_OF_MEMORY, "Failed pa_context_connect()");
+        if (pa_context_connect(__mpContext, NULL, PA_CONTEXT_NOFLAGS, NULL) < 0) {
+            THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_context_connect()");
         }
 
         // LOCK for synchronous connection
-        pa_threaded_mainloop_lock(mpMainloop);
+        pa_threaded_mainloop_lock(__mpMainloop);
 
         // Start mainloop
-        if (pa_threaded_mainloop_start(mpMainloop) < 0) {
-            pa_threaded_mainloop_unlock(mpMainloop);
-            THROW_ERROR_MSG(CAudioError::ERROR_FAILED_OPERATION, "Failed pa_threaded_mainloop_start()");
+        if (pa_threaded_mainloop_start(__mpMainloop) < 0) {
+            pa_threaded_mainloop_unlock(__mpMainloop);
+            THROW_ERROR_MSG(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_threaded_mainloop_start()");
         }
 
         // Connection process is asynchronously
@@ -213,65 +348,76 @@ void CPulseAudioClient::initialize() throw (CAudioError) {
         // If I got a signal, do next processing
         while (true) {
             pa_context_state_t state;
-            state = pa_context_get_state(mpContext);
+            state = pa_context_get_state(__mpContext);
 
             if (state == PA_CONTEXT_READY) {
                 break;
             }
 
             if (!PA_CONTEXT_IS_GOOD(state)) {
-                err = pa_context_errno(mpContext);
-                pa_threaded_mainloop_unlock(mpMainloop);
-                THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INTERNAL_OPERATION, "pa_context's state is not good err:[%d]", err);
+                err = pa_context_errno(__mpContext);
+                pa_threaded_mainloop_unlock(__mpMainloop);
+                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(mpMainloop);
+            pa_threaded_mainloop_wait(__mpMainloop);
         }
 
         // Allocates PA stream
-        pa_sample_spec ss   = mSpec.getSampleSpec();
-        pa_channel_map map  = mSpec.getChannelMap();
+        pa_sample_spec ss   = __mSpec.getSampleSpec();
+        pa_channel_map map  = __mSpec.getChannelMap();
 
-        mpStream = pa_stream_new_with_proplist(mpContext, mSpec.getStreamName(), &ss, &map, mpPropList);
-        if (mpStream == NULL) {
-            pa_threaded_mainloop_unlock(mpMainloop);
-            THROW_ERROR_MSG(CAudioError::ERROR_FAILED_OPERATION, "Failed pa_stream_new_with_proplist()()");
+        __mpStream = pa_stream_new_with_proplist(__mpContext, __mSpec.getStreamName(), &ss, &map, __mpPropList);
+        if (__mpStream == NULL) {
+            pa_threaded_mainloop_unlock(__mpMainloop);
+            THROW_ERROR_MSG(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_new_with_proplist()");
         }
 
         // Sets stream callbacks
-        pa_stream_set_state_callback(mpStream, _streamStateChangeCb, this);
-        pa_stream_set_read_callback(mpStream, _streamCaptureCb, this);
-        pa_stream_set_write_callback(mpStream, _streamPlaybackCb, this);
-        pa_stream_set_latency_update_callback(mpStream, _streamLatencyUpdateCb, this);
+        pa_stream_set_state_callback(__mpStream, __streamStateChangeCb, this);
+        pa_stream_set_read_callback(__mpStream, __streamCaptureCb, this);
+        pa_stream_set_write_callback(__mpStream, __streamPlaybackCb, this);
+        pa_stream_set_latency_update_callback(__mpStream, __streamLatencyUpdateCb, this);
+        pa_stream_set_event_callback(__mpStream, __streamEventCb, this);
+        if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
+            pa_stream_set_started_callback(__mpStream, __streamStartedCb, this);
+            pa_stream_set_underflow_callback(__mpStream, __streamUnderflowCb, this);
+        }
 
         // Connect stream with PA Server
 
-        if (mDirection == STREAM_DIRECTION_PLAYBACK) {
+        if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
             pa_stream_flags_t flags = static_cast<pa_stream_flags_t>(
                     PA_STREAM_INTERPOLATE_TIMING |
                     PA_STREAM_ADJUST_LATENCY     |
+#ifndef DISABLE_MOBILE_BACK_COMP
+                    PA_STREAM_START_CORKED       |
+#endif
                     PA_STREAM_AUTO_TIMING_UPDATE);
 
-            ret = pa_stream_connect_playback(mpStream, NULL, NULL, flags, NULL, NULL);
+            ret = pa_stream_connect_playback(__mpStream, NULL, NULL, flags, NULL, NULL);
         } else {
             pa_stream_flags_t flags = static_cast<pa_stream_flags_t>(
                     PA_STREAM_INTERPOLATE_TIMING |
                     PA_STREAM_ADJUST_LATENCY     |
+#ifndef DISABLE_MOBILE_BACK_COMP
+                    PA_STREAM_START_CORKED       |
+#endif
                     PA_STREAM_AUTO_TIMING_UPDATE);
 
-            ret = pa_stream_connect_record(mpStream, NULL, NULL, flags);
+            ret = pa_stream_connect_record(__mpStream, NULL, NULL, flags);
         }
 
         if (ret != 0) {
-            err = pa_context_errno(mpContext);
-            pa_threaded_mainloop_unlock(mpMainloop);
-            THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_FAILED_OPERATION, "Failed pa_stream_connect() err:[%d]", err);
+            err = pa_context_errno(__mpContext);
+            pa_threaded_mainloop_unlock(__mpMainloop);
+            THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_connect() : err[%d]", err);
         }
 
         while (true) {
             pa_stream_state_t state;
-            state = pa_stream_get_state(mpStream);
+            state = pa_stream_get_state(__mpStream);
 
             if (state == PA_STREAM_READY) {
                 AUDIO_IO_LOGD("STREAM READY");
@@ -279,97 +425,221 @@ void CPulseAudioClient::initialize() throw (CAudioError) {
             }
 
             if (!PA_STREAM_IS_GOOD(state)) {
-                err = pa_context_errno(mpContext);
-                pa_threaded_mainloop_unlock(mpMainloop);
-                THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INTERNAL_OPERATION, "pa_stream's state is not good err:[%d]", err);
+                err = pa_context_errno(__mpContext);
+                pa_threaded_mainloop_unlock(__mpMainloop);
+                if (err == PA_ERR_ACCESS) {
+                    THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_DEVICE_POLICY_RESTRICTION, "pa_stream's state is not good : err[%d]", err);
+                } else {
+                    THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "pa_stream's state is not good : err[%d]", err);
+                }
             }
 
             /* Wait until the stream is ready */
-            pa_threaded_mainloop_wait(mpMainloop);
+            pa_threaded_mainloop_wait(__mpMainloop);
         }
 
         // End of synchronous
-        pa_threaded_mainloop_unlock(mpMainloop);
+        pa_threaded_mainloop_unlock(__mpMainloop);
 
-        mIsInit = true;
-    } catch (CAudioError e) {
+        // __mIsInit = true;  // Moved to __streamStateChangeCb()
+    } catch (CAudioError& e) {
         finalize();
-        throw e;
+        throw;
     }
 }
 
 void CPulseAudioClient::finalize() {
     AUDIO_IO_LOGD("");
-    if (mIsInit == false) {
-        return;
+
+    if (__mpMainloop && isInThread() == false)
+        pa_threaded_mainloop_lock(__mpMainloop);
+
+    /* clear callbacks */
+    if (__mpStream) {
+        if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK)
+            pa_stream_set_write_callback(__mpStream, NULL, NULL);
+        else
+            pa_stream_set_read_callback(__mpStream, NULL, NULL);
+        pa_stream_set_latency_update_callback(__mpStream, NULL, NULL);
+        pa_stream_set_event_callback(__mpStream, NULL, NULL);
+    }
+
+    if (__mpMainloop && isInThread() == false)
+        pa_threaded_mainloop_unlock(__mpMainloop);
+
+    /* Wait for drain complete if draining before finalize */
+    if (__mIsDraining) {
+        unsigned int drain_wait_count = 0;
+        while (__mIsDraining && drain_wait_count++ < drain_wait_max_count) {
+            usleep(drain_wait_interval);
+        }
+        AUDIO_IO_LOGD("wait for drain complete!!!! [%d * %d usec]",
+                      drain_wait_count, drain_wait_interval);
     }
 
-    if (mpMainloop != NULL) {
-        pa_threaded_mainloop_stop(mpMainloop);
+    if (__mpMainloop != NULL) {
+        pa_threaded_mainloop_stop(__mpMainloop);
     }
-    if (mpStream != NULL) {
-        pa_stream_disconnect(mpStream);
-        mpStream = NULL;
+
+    if (__mpStream != NULL) {
+        pa_stream_disconnect(__mpStream);
+        pa_stream_unref(__mpStream);
+        __mpStream = NULL;
     }
 
-    if (mpContext != NULL) {
-        pa_context_disconnect(mpContext);
-        pa_context_unref(mpContext);
-        mpContext = NULL;
+    if (__mpContext != NULL) {
+        pa_context_disconnect(__mpContext);
+        pa_context_unref(__mpContext);
+        __mpContext = NULL;
     }
 
-    if (mpMainloop != NULL) {
-        pa_threaded_mainloop_free(mpMainloop);
-        mpMainloop = NULL;
+    if (__mpMainloop != NULL) {
+        pa_threaded_mainloop_free(__mpMainloop);
+        __mpMainloop = NULL;
     }
 
-    if (mpPropList != NULL) {
-        pa_proplist_free(mpPropList);
-        mpPropList = NULL;
+    if (__mpPropList != NULL) {
+        pa_proplist_free(__mpPropList);
+        __mpPropList = NULL;
     }
 
-    mIsInit = false;
+    __mIsInit = false;
 }
 
-int CPulseAudioClient::peek(const void** data, size_t* length) throw (CAudioError) {
-    if (mIsInit == false) {
-        THROW_ERROR_MSG(CAudioError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
+int CPulseAudioClient::read(void* buffer, size_t length) {
+    if (__mIsInit == false) {
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
     }
 
+    checkRunningState();
+
+    if (buffer == NULL) {
+        THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "The parameter is invalid : buffer[%p]", buffer);
+    }
+
+    if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
+    }
+
+    size_t lengthIter = length;
+    int ret = 0;
+
+    __mIsUsedSyncRead = true;
+
+    try {
+        pa_threaded_mainloop_lock(__mpMainloop);
+
+        while (lengthIter > 0) {
+            size_t l;
+
+            while (__mpSyncReadDataPtr == NULL) {
+                ret = pa_stream_peek(__mpStream, &__mpSyncReadDataPtr, &__mSyncReadLength);
+                if (ret != 0) {
+                    THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pa_stream_peek() : ret[%d]", ret);
+                }
+
+                if (__mSyncReadLength <= 0) {
+#ifdef _AUDIO_IO_DEBUG_TIMING_
+                    AUDIO_IO_LOGD("readLength(%d byte) is not valid. wait...", __mSyncReadLength);
+#endif
+                    pa_threaded_mainloop_wait(__mpMainloop);
+                } else if (__mpSyncReadDataPtr == NULL) {
+                    // Data peeked, but it doesn't have any data
+                    ret = pa_stream_drop(__mpStream);
+                    if (ret != 0) {
+                        THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pa_stream_drop() : ret[%d]", ret);
+                    }
+                } else {
+                    __mSyncReadIndex = 0;
+                }
+            }
+
+            if (__mSyncReadLength < lengthIter) {
+                l = __mSyncReadLength;
+            } else {
+                l = lengthIter;
+            }
+
+            // Copy partial pcm data on out parameter
+#ifdef _AUDIO_IO_DEBUG_TIMING_
+            AUDIO_IO_LOGD("memcpy() that a peeked buffer[0x%x], index[%d], length[%d] on out buffer", (const uint8_t*)(__mpSyncReadDataPtr) + __mSyncReadIndex, __mSyncReadIndex, l);
+#endif
+            memcpy(buffer, (const uint8_t*)__mpSyncReadDataPtr + __mSyncReadIndex, l);
+
+            // Move next position
+            buffer = (uint8_t*)buffer + l;
+            lengthIter -= l;
+
+            // Adjusts the rest length
+            __mSyncReadIndex  += l;
+            __mSyncReadLength -= l;
+
+            if (__mSyncReadLength == 0) {
 #ifdef _AUDIO_IO_DEBUG_TIMING_
-    AUDIO_IO_LOGD("data:[%p], length:[%p]", data, length);
+                AUDIO_IO_LOGD("__mSyncReadLength is zero, do drop()");
 #endif
+                ret = pa_stream_drop(__mpStream);
+                if (ret != 0) {
+                    THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pa_stream_drop() : ret[%d]", ret);
+                }
+
+                // Reset the internal pointer
+                __mpSyncReadDataPtr = NULL;
+                __mSyncReadLength   = 0;
+                __mSyncReadIndex    = 0;
+            }
+        }  // End of while (lengthIter > 0)
+
+        pa_threaded_mainloop_unlock(__mpMainloop);
+        __mIsUsedSyncRead = false;
+    } catch (CAudioError& e) {
+        pa_threaded_mainloop_unlock(__mpMainloop);
+        __mIsUsedSyncRead = false;
+        throw;
+    }
+
+    return length;
+}
+
+int CPulseAudioClient::peek(const void** buffer, size_t* length) {
+    if (__mIsInit == false) {
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
+    }
 
     checkRunningState();
 
-    if (data == NULL || length == NULL) {
-        THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_INVALID_ARGUMENT, "The parameter is invalid - data:%p, length:%p", data, length);
+    if (buffer == NULL || length == NULL) {
+        THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "The parameter is invalid : buffer[%p], length[%p]", buffer, length);
     }
 
-    if (mDirection == STREAM_DIRECTION_PLAYBACK) {
-        THROW_ERROR_MSG(CAudioError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
+    if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
     }
 
     int ret = 0;
 
     if (isInThread() == false) {
-        pa_threaded_mainloop_lock(mpMainloop);
-        ret = pa_stream_peek(mpStream, data, length);
-        pa_threaded_mainloop_unlock(mpMainloop);
+        pa_threaded_mainloop_lock(__mpMainloop);
+        ret = pa_stream_peek(__mpStream, buffer, length);
+        pa_threaded_mainloop_unlock(__mpMainloop);
     } else {
-        ret = pa_stream_peek(mpStream, data, length);
+        ret = pa_stream_peek(__mpStream, buffer, length);
     }
 
+#ifdef _AUDIO_IO_DEBUG_TIMING_
+    AUDIO_IO_LOGD("buffer[%p], length[%d]", *buffer, *length);
+#endif
+
     if (ret < 0) {
-        THROW_ERROR_MSG(CAudioError::ERROR_FAILED_OPERATION, "Failed pa_stream_peek()");
+        THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_peek() : err[%d]", ret);
     }
 
     return ret;
 }
 
-int CPulseAudioClient::drop() throw (CAudioError) {
-    if (mIsInit == false) {
-        THROW_ERROR_MSG(CAudioError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
+int CPulseAudioClient::drop() {
+    if (__mIsInit == false) {
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
     }
 
 #ifdef _AUDIO_IO_DEBUG_TIMING_
@@ -378,90 +648,113 @@ int CPulseAudioClient::drop() throw (CAudioError) {
 
     checkRunningState();
 
-    if (mDirection == STREAM_DIRECTION_PLAYBACK) {
-        THROW_ERROR_MSG(CAudioError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
+    if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
     }
 
     int ret = 0;
 
     if (isInThread() == false) {
-        pa_threaded_mainloop_lock(mpMainloop);
-        ret = pa_stream_drop(mpStream);
-        pa_threaded_mainloop_unlock(mpMainloop);
+        pa_threaded_mainloop_lock(__mpMainloop);
+        ret = pa_stream_drop(__mpStream);
+        pa_threaded_mainloop_unlock(__mpMainloop);
     } else {
-        ret = pa_stream_drop(mpStream);
+        ret = pa_stream_drop(__mpStream);
     }
 
     if (ret < 0) {
-        THROW_ERROR_MSG(CAudioError::ERROR_FAILED_OPERATION, "Failed pa_stream_drop()");
+        THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_drop() : err[%d]", ret);
     }
 
     return ret;
 }
 
-int CPulseAudioClient::write(const void* data, size_t length) throw (CAudioError) {
-    if (mIsInit == false) {
-        THROW_ERROR_MSG(CAudioError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
-    }
-
-#ifdef _AUDIO_IO_DEBUG_TIMING_
-    AUDIO_IO_LOGD("data[%p], length:[%d]", data, length);
-#endif
-
-    checkRunningState();
-
-    if (data == NULL || length < 0) {
-        THROW_ERROR_MSG(CAudioError::ERROR_INVALID_ARGUMENT, "The parameter is invalid");
+int CPulseAudioClient::write(const void* data, size_t length) {
+    if (data == NULL) {
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_ARGUMENT, "The parameter is invalid");
     }
 
-    if (mDirection == STREAM_DIRECTION_RECORD) {
-        THROW_ERROR_MSG(CAudioError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
+    if (__mDirection == EStreamDirection::STREAM_DIRECTION_RECORD) {
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "The Playback client couldn't use this function");
     }
 
     int ret = 0;
 
+#ifdef _AUDIO_IO_DEBUG_TIMING_
+    AUDIO_IO_LOGD("data[%p], length[%d], First[%d]", data, length, __mIsFirstStream);
+#endif
+
     if (isInThread() == false) {
-        pa_threaded_mainloop_lock(mpMainloop);
-        ret = pa_stream_write(mpStream, data, length, NULL, 0LL, PA_SEEK_RELATIVE);
-        pa_threaded_mainloop_unlock(mpMainloop);
+        pa_threaded_mainloop_lock(__mpMainloop);
+        if (pa_stream_is_corked(__mpStream)) {
+            AUDIO_IO_LOGW("stream is corked...do uncork here first!!!!");
+            pa_operation_unref(pa_stream_cork(__mpStream, 0, NULL, this));
+        }
+
+        ret = pa_stream_write(__mpStream, data, length, NULL, 0LL, PA_SEEK_RELATIVE);
+        pa_threaded_mainloop_unlock(__mpMainloop);
     } else {
-        ret = pa_stream_write(mpStream, data, length, NULL, 0LL, PA_SEEK_RELATIVE);
+        if (pa_stream_is_corked(__mpStream)) {
+            AUDIO_IO_LOGW("stream is corked...do uncork here first!!!!");
+            pa_operation_unref(pa_stream_cork(__mpStream, 0, NULL, this));
+        }
+
+        if (__mIsFirstStream) {
+            const pa_buffer_attr* attr = pa_stream_get_buffer_attr(__mpStream);
+            uint32_t prebuf = attr->prebuf;
+            // Compensate amount of prebuf in first stream callback when audio-io use prebuf(-1)
+            // Because a stream will never start when an application wrote less than prebuf at first
+            if (length < prebuf) {
+                char* dummy = new char[prebuf - length];
+                memset(dummy, 0, prebuf - length);
+                pa_stream_write(__mpStream, dummy, prebuf - length, NULL, 0LL, PA_SEEK_RELATIVE);
+                delete [] dummy;
+            }
+            __mIsFirstStream = false;
+            AUDIO_IO_LOGW("FIRST STREAM CALLBACK : length[%d], prebuf[%d], dummy[%d]",
+                          length, prebuf, (length < prebuf) ? prebuf - length : 0);
+        }
+        ret = pa_stream_write(__mpStream, data, length, NULL, 0LL, PA_SEEK_RELATIVE);
     }
 
     if (ret < 0) {
-        THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_FAILED_OPERATION, "Failed pa_stream_write() err:%d", ret);
+        THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_write() : err[%d]", ret);
     }
 
     return ret;
 }
 
-void CPulseAudioClient::cork(bool cork) throw (CAudioError) {
-    AUDIO_IO_LOGD("bool cork:%d", cork);
+void CPulseAudioClient::cork(bool cork) {
+    AUDIO_IO_LOGD("cork[%d]", cork);
 
-    if (mIsInit == false) {
-        THROW_ERROR_MSG(CAudioError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
+    if (__mIsInit == false) {
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
     }
 
     if (isInThread() == true) {
-        THROW_ERROR_MSG(CAudioError::ERROR_NOT_SUPPORTED, "This operation is not supported in callback");
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "This operation is not supported in callback");
     }
 
     checkRunningState();
 
+    // Set __mIsFirstStream flag when uncork(resume) stream, because prebuf will be enable again
+    if (cork == false)
+        __mIsFirstStream = true;
+
     if (isInThread() == false) {
-        pa_threaded_mainloop_lock(mpMainloop);
-        pa_operation_unref(pa_stream_cork(mpStream, static_cast<int>(cork), _successStreamCb, this));
-        pa_threaded_mainloop_unlock(mpMainloop);
+        pa_threaded_mainloop_lock(__mpMainloop);
+        pa_operation_unref(pa_stream_cork(__mpStream, static_cast<int>(cork), __successStreamCb, this));
+        pa_threaded_mainloop_unlock(__mpMainloop);
     } else {
-        pa_operation_unref(pa_stream_cork(mpStream, static_cast<int>(cork), _successStreamCb, this));
+        pa_operation_unref(pa_stream_cork(__mpStream, static_cast<int>(cork), __successStreamCb, this));
     }
 
     return;
 }
 
-bool CPulseAudioClient::isCorked() throw (CAudioError) {
-    if (mIsInit == false) {
-        THROW_ERROR_MSG(CAudioError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
+bool CPulseAudioClient::isCorked() {
+    if (__mIsInit == false) {
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
     }
 
     checkRunningState();
@@ -469,94 +762,114 @@ bool CPulseAudioClient::isCorked() throw (CAudioError) {
     int isCork = 0;
 
     if (isInThread() == false) {
-        pa_threaded_mainloop_lock(mpMainloop);
-        isCork = pa_stream_is_corked(mpStream);
-        pa_threaded_mainloop_unlock(mpMainloop);
+        pa_threaded_mainloop_lock(__mpMainloop);
+        isCork = pa_stream_is_corked(__mpStream);
+        pa_threaded_mainloop_unlock(__mpMainloop);
     } else {
-        isCork = pa_stream_is_corked(mpStream);
+        isCork = pa_stream_is_corked(__mpStream);
     }
 
-    AUDIO_IO_LOGD("isCork:%d", isCork);
+#ifdef _AUDIO_IO_DEBUG_TIMING_
+    AUDIO_IO_LOGD("isCork[%d]", isCork);
+#endif
     return static_cast<bool>(isCork);
 }
 
-bool CPulseAudioClient::drain() throw (CAudioError) {
-    AUDIO_IO_LOGD("drain");
-
-    if (mIsInit == false) {
-        THROW_ERROR_MSG(CAudioError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
+bool CPulseAudioClient::drain() {
+    if (__mIsInit == false) {
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
     }
 
     checkRunningState();
 
+    if (isCorked()) {
+        AUDIO_IO_LOGW("Corked...");
+        return true;
+    }
+
+    if (__mIsDraining) {
+        AUDIO_IO_LOGW("already draining...");
+        return true;
+    }
+
+    if (!__mIsStarted) {
+        AUDIO_IO_LOGW("stream not started yet...skip drain");
+        return true;
+    }
+
     if (isInThread() == false) {
-        pa_threaded_mainloop_lock(mpMainloop);
-        pa_operation_unref(pa_stream_drain(mpStream, _successStreamCb, this));
-        pa_threaded_mainloop_unlock(mpMainloop);
+        AUDIO_IO_LOGD("drain");
+        pa_threaded_mainloop_lock(__mpMainloop);
+        pa_operation* o = pa_stream_drain(__mpStream, __successDrainCb, this);
+        __mIsDraining = true;
+        while (pa_operation_get_state(o) == PA_OPERATION_RUNNING) {
+            pa_threaded_mainloop_wait(__mpMainloop);
+        }
+        pa_operation_unref(o);
+        pa_threaded_mainloop_unlock(__mpMainloop);
+        AUDIO_IO_LOGD("drain done");
     } else {
-        pa_operation_unref(pa_stream_drain(mpStream, _successStreamCb, this));
+        AUDIO_IO_LOGD("drain in thread");
+        pa_operation_unref(pa_stream_drain(__mpStream, __successDrainCbInThread, this));
+        __mIsDraining = true;
     }
 
     return true;
 }
 
-bool CPulseAudioClient::flush() throw (CAudioError) {
-    AUDIO_IO_LOGD("flush");
-
-    if (mIsInit == false) {
-        THROW_ERROR_MSG(CAudioError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
+bool CPulseAudioClient::flush() {
+    if (__mIsInit == false) {
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
     }
 
     checkRunningState();
 
     if (isInThread() == false) {
-        pa_threaded_mainloop_lock(mpMainloop);
-        pa_operation_unref(pa_stream_flush(mpStream, _successStreamCb, this));
-        pa_threaded_mainloop_unlock(mpMainloop);
+        AUDIO_IO_LOGD("flush");
+        pa_threaded_mainloop_lock(__mpMainloop);
+        pa_operation_unref(pa_stream_flush(__mpStream, __successStreamCb, this));
+        pa_threaded_mainloop_unlock(__mpMainloop);
     } else {
-        pa_operation_unref(pa_stream_flush(mpStream, _successStreamCb, this));
+        AUDIO_IO_LOGD("flush in thread");
+        pa_operation_unref(pa_stream_flush(__mpStream, __successStreamCb, this));
     }
 
     return true;
 }
 
-size_t CPulseAudioClient::getWritableSize() throw (CAudioError) {
-    if (mIsInit == false) {
-        THROW_ERROR_MSG(CAudioError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
+size_t CPulseAudioClient::getWritableSize() {
+    if (__mIsInit == false) {
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
     }
 
     checkRunningState();
 
-    if (mDirection != STREAM_DIRECTION_PLAYBACK) {
-        THROW_ERROR_MSG(CAudioError::ERROR_NOT_SUPPORTED, "This client is used for Playback");
+    if (__mDirection != EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "This client is used for Playback");
     }
 
     size_t ret = 0;
 
     if (isInThread() == false) {
-        pa_threaded_mainloop_lock(mpMainloop);
-        ret = pa_stream_writable_size(mpStream);
-        pa_threaded_mainloop_unlock(mpMainloop);
+        pa_threaded_mainloop_lock(__mpMainloop);
+        ret = pa_stream_writable_size(__mpStream);
+        pa_threaded_mainloop_unlock(__mpMainloop);
     } else {
-        ret = pa_stream_writable_size(mpStream);
+        ret = pa_stream_writable_size(__mpStream);
     }
 
     return ret;
 }
 
-void CPulseAudioClient::checkRunningState() throw (CAudioError) {
-    if (mIsInit == false) {
-        THROW_ERROR_MSG(CAudioError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
+void CPulseAudioClient::checkRunningState() {
+    if (__mpContext == NULL || PA_CONTEXT_IS_GOOD(pa_context_get_state(__mpContext)) == 0) {
+        THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_INITIALIZED, "The context[%p] is not created or not good state", __mpContext);
     }
-
-    if (mpContext == NULL || PA_CONTEXT_IS_GOOD(pa_context_get_state(mpContext)) == 0) {
-        THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_NOT_INITIALIZED, "The context[%p] is not created or not good state", mpContext);
-    }
-    if (mpStream == NULL || PA_STREAM_IS_GOOD(pa_stream_get_state(mpStream)) == 0) {
-        THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_NOT_INITIALIZED, "The stream[%p] is not created or not good state", mpStream);
+    if (__mpStream == NULL || PA_STREAM_IS_GOOD(pa_stream_get_state(__mpStream)) == 0) {
+        THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_INITIALIZED, "The stream[%p] is not created or not good state", __mpStream);
     }
-    if (pa_context_get_state(mpContext) != PA_CONTEXT_READY || pa_stream_get_state(mpStream)   != PA_STREAM_READY) {
-        THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_NOT_INITIALIZED, "The context[%p] or stream[%p] state is not ready", mpContext, mpStream);
+    if (pa_context_get_state(__mpContext) != PA_CONTEXT_READY || pa_stream_get_state(__mpStream) != PA_STREAM_READY) {
+        THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_INITIALIZED, "The context[%p] or stream[%p] state is not ready", __mpContext, __mpStream);
     }
 
 #ifdef _AUDIO_IO_DEBUG_TIMING_
@@ -564,46 +877,42 @@ void CPulseAudioClient::checkRunningState() throw (CAudioError) {
 #endif
 }
 
-bool CPulseAudioClient::isInThread() throw (CAudioError) {
-    if (mIsInit == false) {
-        THROW_ERROR_MSG(CAudioError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
-    }
-
-    int ret = pa_threaded_mainloop_in_thread(mpMainloop);
+bool CPulseAudioClient::isInThread() {
+    int ret = pa_threaded_mainloop_in_thread(__mpMainloop);
 
 #ifdef _AUDIO_IO_DEBUG_TIMING_
-    AUDIO_IO_LOGD("isInThread : [%d][TRUE:1][FALSE:0]", ret);
+    AUDIO_IO_LOGD("isInThread[%d]", ret);
 #endif
     return static_cast<bool>(ret);
 }
 
-size_t CPulseAudioClient::getReadableSize() throw (CAudioError) {
-    if (mIsInit == false) {
-        THROW_ERROR_MSG(CAudioError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
+size_t CPulseAudioClient::getReadableSize() {
+    if (__mIsInit == false) {
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
     }
 
     checkRunningState();
 
-    if (mDirection != STREAM_DIRECTION_RECORD) {
-        THROW_ERROR_MSG(CAudioError::ERROR_NOT_SUPPORTED, "This client is used for Capture");
+    if (__mDirection != EStreamDirection::STREAM_DIRECTION_RECORD) {
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_SUPPORTED, "This client is used for Capture");
     }
 
     size_t ret = 0;
 
     if (isInThread() == false) {
-        pa_threaded_mainloop_lock(mpMainloop);
-        ret = pa_stream_writable_size(mpStream);
-        pa_threaded_mainloop_unlock(mpMainloop);
+        pa_threaded_mainloop_lock(__mpMainloop);
+        ret = pa_stream_writable_size(__mpStream);
+        pa_threaded_mainloop_unlock(__mpMainloop);
     } else {
-        ret = pa_stream_writable_size(mpStream);
+        ret = pa_stream_writable_size(__mpStream);
     }
 
     return ret;
 }
 
-size_t CPulseAudioClient::getBufferSize() throw (CAudioError) {
-    if (mIsInit == false) {
-        THROW_ERROR_MSG(CAudioError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
+size_t CPulseAudioClient::getBufferSize() {
+    if (__mIsInit == false) {
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
     }
 
     checkRunningState();
@@ -612,39 +921,39 @@ size_t CPulseAudioClient::getBufferSize() throw (CAudioError) {
 
     try {
         if (isInThread() == false) {
-            pa_threaded_mainloop_lock(mpMainloop);
+            pa_threaded_mainloop_lock(__mpMainloop);
         }
 
-        const pa_buffer_attr* attr = pa_stream_get_buffer_attr(mpStream);
+        const pa_buffer_attr* attr = pa_stream_get_buffer_attr(__mpStream);
         if (attr == NULL) {
-            int _err = pa_context_errno(mpContext);
-            THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_FAILED_OPERATION, "Failed pa_stream_get_buffer_attr() err:%d", _err);
+            int _err = pa_context_errno(__mpContext);
+            THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_get_buffer_attr() : err[%d]", _err);
         }
 
-        if (mDirection == STREAM_DIRECTION_PLAYBACK) {
+        if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
             ret = attr->tlength;
-            AUDIO_IO_LOGD("PLAYBACK buffer size : %d", ret);
+            AUDIO_IO_LOGD("PLAYBACK buffer size[%d]", ret);
         } else {
             ret = attr->fragsize;
-            AUDIO_IO_LOGD("RECORD buffer size : %d", ret);
+            AUDIO_IO_LOGD("RECORD buffer size[%d]", ret);
         }
-    } catch (CAudioError err) {
+    } catch (CAudioError& e) {
         if (isInThread() == false) {
-            pa_threaded_mainloop_unlock(mpMainloop);
+            pa_threaded_mainloop_unlock(__mpMainloop);
         }
-        throw err;
+        throw;
     }
 
     if (isInThread() == false) {
-        pa_threaded_mainloop_unlock(mpMainloop);
+        pa_threaded_mainloop_unlock(__mpMainloop);
     }
 
     return ret;
 }
 
-pa_usec_t CPulseAudioClient::getLatency() throw (CAudioError) {
-    if (mIsInit == false) {
-        THROW_ERROR_MSG(CAudioError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
+pa_usec_t CPulseAudioClient::getLatency() {
+    if (__mIsInit == false) {
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
     }
 
     checkRunningState();
@@ -653,44 +962,44 @@ pa_usec_t CPulseAudioClient::getLatency() throw (CAudioError) {
     int negative  = 0;
 
     if (isInThread() == false) {
-        if (pa_stream_get_latency(mpStream, &ret, &negative) < 0) {
-            int _err = pa_context_errno(mpContext);
+        if (pa_stream_get_latency(__mpStream, &ret, &negative) < 0) {
+            int _err = pa_context_errno(__mpContext);
             if (_err != PA_ERR_NODATA) {
-                THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_FAILED_OPERATION, "Failed pa_stream_get_latency() err:%d", _err);
+                THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_get_latency() : err[%d]", _err);
             }
         }
         return negative ? 0 : ret;
     }
 
-        pa_threaded_mainloop_lock(mpMainloop);
+    pa_threaded_mainloop_lock(__mpMainloop);
 
     try {
         while (true) {
-            if (pa_stream_get_latency(mpStream, &ret, &negative) >= 0) {
+            if (pa_stream_get_latency(__mpStream, &ret, &negative) >= 0) {
                 break;
             }
 
-            int _err = pa_context_errno(mpContext);
+            int _err = pa_context_errno(__mpContext);
             if (_err != PA_ERR_NODATA) {
-                THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_FAILED_OPERATION, "Failed pa_stream_get_latency() err:%d", _err);
+                THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_get_latency() : err[%d]", _err);
             }
 
             /* Wait until latency data is available again */
-            pa_threaded_mainloop_wait(mpMainloop);
+            pa_threaded_mainloop_wait(__mpMainloop);
         }
-    } catch (CAudioError e) {
-        pa_threaded_mainloop_unlock(mpMainloop);
-        throw e;
+    } catch (CAudioError& e) {
+        pa_threaded_mainloop_unlock(__mpMainloop);
+        throw;
     }
 
-    pa_threaded_mainloop_unlock(mpMainloop);
+    pa_threaded_mainloop_unlock(__mpMainloop);
 
     return negative ? 0 : ret;
 }
 
-pa_usec_t CPulseAudioClient::getFinalLatency() throw (CAudioError) {
-    if (mIsInit == false) {
-        THROW_ERROR_MSG(CAudioError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
+pa_usec_t CPulseAudioClient::getFinalLatency() {
+    if (__mIsInit == false) {
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
     }
 
     checkRunningState();
@@ -700,48 +1009,48 @@ pa_usec_t CPulseAudioClient::getFinalLatency() throw (CAudioError) {
 
     try {
         if (isInThread() == false) {
-            pa_threaded_mainloop_lock(mpMainloop);
+            pa_threaded_mainloop_lock(__mpMainloop);
         }
 
-        ver = pa_context_get_server_protocol_version(mpContext);
+        ver = pa_context_get_server_protocol_version(__mpContext);
         if (ver >= 13) {
-            const pa_buffer_attr* buffer_attr = pa_stream_get_buffer_attr(mpStream);
-            const pa_sample_spec* sample_spec = pa_stream_get_sample_spec(mpStream);
-            const pa_timing_info* timing_info = pa_stream_get_timing_info(mpStream);
+            const pa_buffer_attr* buffer_attr = pa_stream_get_buffer_attr(__mpStream);
+            const pa_sample_spec* sample_spec = pa_stream_get_sample_spec(__mpStream);
+            const pa_timing_info* timing_info = pa_stream_get_timing_info(__mpStream);
 
             if (buffer_attr == NULL || sample_spec == NULL || timing_info == NULL) {
-                THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_OUT_OF_MEMORY, "Failed to get buffer_attr[%p] or sample_spec[%p] or timing_info[%p] from a pa_stream",
+                THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed to get buffer_attr[%p] or sample_spec[%p] or timing_info[%p] from a pa_stream",
                         buffer_attr, sample_spec, timing_info);
             }
 
-            if (mDirection == STREAM_DIRECTION_PLAYBACK) {
+            if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
                 ret = (pa_bytes_to_usec(buffer_attr->tlength, sample_spec) + timing_info->configured_sink_usec);
-                AUDIO_IO_LOGD("FINAL PLAYBACK LATENCY : %d", ret);
+                AUDIO_IO_LOGD("FINAL PLAYBACK LATENCY[%" PRIu64 "]", ret);
             } else {
                 ret = (pa_bytes_to_usec(buffer_attr->fragsize, sample_spec) + timing_info->configured_source_usec);
-                AUDIO_IO_LOGD("FINAL RECORD LATENCY : %d", ret);
+                AUDIO_IO_LOGD("FINAL RECORD LATENCY[%" PRIu64 "]", ret);
             }
         } else {
-            THROW_ERROR_MSG_FORMAT(CAudioError::ERROR_NOT_SUPPORTED, "This version(ver.%d) is not supported", ver);
+            THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED, "This version(ver.%d) is not supported", ver);
         }
 
         if (isInThread() == false) {
-            pa_threaded_mainloop_unlock(mpMainloop);
+            pa_threaded_mainloop_unlock(__mpMainloop);
         }
-    } catch (CAudioError e) {
+    } catch (CAudioError& e) {
         if (isInThread() == false) {
-            pa_threaded_mainloop_unlock(mpMainloop);
+            pa_threaded_mainloop_unlock(__mpMainloop);
         }
-        throw e;
+        throw;
     }
 
     return ret;
 }
 
 CPulseAudioClient::EStreamDirection CPulseAudioClient::getStreamDirection() {
-    return mDirection;
+    return __mDirection;
 }
 
 CPulseStreamSpec CPulseAudioClient::getStreamSpec() {
-    return mSpec;
+    return __mSpec;
 }