Merge branch 'tizen' into tizen_line_coverage
[platform/core/api/audio-io.git] / src / cpp / CPulseAudioClient.cpp
index 8c0d379..4423dda 100644 (file)
@@ -15,9 +15,9 @@
  */
 
 
-#include <mm.h>
 #include "CAudioIODef.h"
 #include <unistd.h>
+#include <inttypes.h>
 #ifdef ENABLE_DPM
 #include <dpm/restriction.h>
 #endif
@@ -51,7 +51,8 @@ CPulseAudioClient::CPulseAudioClient(
     __mSyncReadLength(0),
     __mIsUsedSyncRead(false),
     __mIsFirstStream(false),
-    __mIsDraining(false) {
+    __mIsDraining(false),
+    __mIsStarted(false) {
 }
 
 CPulseAudioClient::~CPulseAudioClient() {
@@ -83,6 +84,7 @@ void CPulseAudioClient::__contextStateChangeCb(pa_context* c, void* user_data) {
     }
 }
 
+//LCOV_EXCL_START
 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);
@@ -112,6 +114,7 @@ static bool __is_microphone_restricted(void) {
 #endif
     return (state ? false : true);
 }
+//LCOV_EXCL_STOP
 
 void CPulseAudioClient::__streamStateChangeCb(pa_stream* s, void* user_data) {
     assert(s);
@@ -130,11 +133,13 @@ void CPulseAudioClient::__streamStateChangeCb(pa_stream* s, void* user_data) {
         break;
 
     case PA_STREAM_FAILED:
+//LCOV_EXCL_START
         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;
+//LCOV_EXCL_STOP
 
     case PA_STREAM_TERMINATED:
         AUDIO_IO_LOGD("The stream is terminated");
@@ -163,6 +168,15 @@ void CPulseAudioClient::__streamCaptureCb(pa_stream* s, size_t length, void* use
     pClient->__mpListener->onStream(pClient, length);
 }
 
+#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);
@@ -170,20 +184,31 @@ void CPulseAudioClient::__streamPlaybackCb(pa_stream* s, size_t length, void* us
     CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
     assert(pClient->__mpListener);
 
+#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[%zu]", length);
+        __dummy_write(s, length);
+        return;
+    }
+    if (pClient->isCorked()) {
+        AUDIO_IO_LOGD("Occurred this listener when an out stream is CORKED : Write dummy, length[%zu]", length);
+        __dummy_write(s, length);
+        return;
+    }
+#endif
+
     pClient->__mpListener->onStream(pClient, length);
 
+#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->__mIsFirstStream) {
-        AUDIO_IO_LOGD("Write dummy, length [%d]", length);
-
-        char* dummy = new char[length];
-        memset(dummy, 0, length);
-        pa_stream_write(s, dummy, length, NULL, 0LL, PA_SEEK_RELATIVE);
-        delete [] dummy;
-
+    if (pClient->__mSpec.getStreamLatency() == CPulseStreamSpec::EStreamLatency::STREAM_LATENCY_OUTPUT_DEFAULT_ASYNC &&
+        pClient->__mIsFirstStream) {
+        AUDIO_IO_LOGW("[async] Write dummy of length[%zu] 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) {
@@ -195,6 +220,29 @@ void CPulseAudioClient::__streamLatencyUpdateCb(pa_stream* s, void* user_data) {
     pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
 }
 
+void CPulseAudioClient::__streamStartedCb(pa_stream* s, void* user_data) {
+    assert(s);
+    assert(user_data);
+
+    CPulseAudioClient* pClient = static_cast<CPulseAudioClient*>(user_data);
+
+    AUDIO_IO_LOGD("stream %p started.", pClient);
+
+    pClient->__mIsStarted = true;
+}
+
+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;
+}
+
+//LCOV_EXCL_START
 void CPulseAudioClient::__streamEventCb(pa_stream* s, const char *name, pa_proplist *pl, void *user_data) {
     assert(s);
     assert(user_data);
@@ -206,7 +254,7 @@ void CPulseAudioClient::__streamEventCb(pa_stream* s, const char *name, pa_propl
         pa_operation_unref(pa_stream_cork(pClient->__mpStream, 1, NULL, NULL));
     }
 }
-
+//LCOV_EXCL_STOP
 
 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);
@@ -219,6 +267,7 @@ void CPulseAudioClient::__successStreamCb(pa_stream* s, int success, void* user_
     pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
 }
 
+//LCOV_EXCL_START
 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);
@@ -228,6 +277,7 @@ void CPulseAudioClient::__successDrainCbInThread(pa_stream* s, int success, void
     pClient->__mIsOperationSuccess = static_cast<bool>(success);
     pClient->__mIsDraining = false;
 }
+//LCOV_EXCL_STOP
 
 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);
@@ -241,7 +291,7 @@ void CPulseAudioClient::__successDrainCb(pa_stream* s, int success, void* user_d
     pa_threaded_mainloop_signal(pClient->__mpMainloop, 0);
 }
 
-void CPulseAudioClient::initialize() throw(CAudioError) {
+void CPulseAudioClient::initialize() {
     if (__mIsInit == true) {
         return;
     }
@@ -253,7 +303,7 @@ void CPulseAudioClient::initialize() throw(CAudioError) {
         // Allocates PA proplist
         __mpPropList = pa_proplist_new();
         if (__mpPropList == NULL) {
-            THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_proplist_new()");
+            THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_proplist_new()"); //LCOV_EXCL_LINE
         }
 
         // Adds values on proplist for delivery to PULSEAUDIO
@@ -268,19 +318,19 @@ void CPulseAudioClient::initialize() throw(CAudioError) {
         }
 
         // Adds latency on proplist for delivery to PULSEAUDIO
-        AUDIO_IO_LOGD("LATENCY : %s[%d]", __mSpec.getStreamLatencyToString(), __mSpec.getStreamLatency());
+        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::EError::ERROR_OUT_OF_MEMORY, "Failed pa_threaded_mainloop_new()");
+            THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_threaded_mainloop_new()"); //LCOV_EXCL_LINE
         }
 
         // Allocates PA context
         __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()");
+            THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_context_new()"); //LCOV_EXCL_LINE
         }
 
         // Sets context state changed callback
@@ -288,7 +338,7 @@ void CPulseAudioClient::initialize() throw(CAudioError) {
 
         // Connects this client with PA server
         if (pa_context_connect(__mpContext, NULL, PA_CONTEXT_NOFLAGS, NULL) < 0) {
-            THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_context_connect()");
+            THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_context_connect()"); //LCOV_EXCL_LINE
         }
 
         // LOCK for synchronous connection
@@ -297,7 +347,7 @@ void CPulseAudioClient::initialize() throw(CAudioError) {
         // Start mainloop
         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()");
+            THROW_ERROR_MSG(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_threaded_mainloop_start()"); //LCOV_EXCL_LINE
         }
 
         // Connection process is asynchronously
@@ -312,9 +362,11 @@ void CPulseAudioClient::initialize() throw(CAudioError) {
             }
 
             if (!PA_CONTEXT_IS_GOOD(state)) {
+//LCOV_EXCL_START
                 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);
+//LCOV_EXCL_STOP
             }
 
             /* Wait until the context is ready */
@@ -327,8 +379,10 @@ void CPulseAudioClient::initialize() throw(CAudioError) {
 
         __mpStream = pa_stream_new_with_proplist(__mpContext, __mSpec.getStreamName(), &ss, &map, __mpPropList);
         if (__mpStream == NULL) {
+//LCOV_EXCL_START
             pa_threaded_mainloop_unlock(__mpMainloop);
             THROW_ERROR_MSG(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_new_with_proplist()");
+//LCOV_EXCL_STOP
         }
 
         // Sets stream callbacks
@@ -337,6 +391,10 @@ void CPulseAudioClient::initialize() throw(CAudioError) {
         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
 
@@ -344,6 +402,9 @@ void CPulseAudioClient::initialize() throw(CAudioError) {
             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);
@@ -351,15 +412,20 @@ void CPulseAudioClient::initialize() throw(CAudioError) {
             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);
         }
 
         if (ret != 0) {
+//LCOV_EXCL_START
             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);
+//LCOV_EXCL_STOP
         }
 
         while (true) {
@@ -377,7 +443,7 @@ void CPulseAudioClient::initialize() throw(CAudioError) {
                 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);
+                    THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "pa_stream's state is not good : err[%d]", err); //LCOV_EXCL_LINE
                 }
             }
 
@@ -389,25 +455,30 @@ void CPulseAudioClient::initialize() throw(CAudioError) {
         pa_threaded_mainloop_unlock(__mpMainloop);
 
         // __mIsInit = true;  // Moved to __streamStateChangeCb()
-    } catch (CAudioError e) {
+    } 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 (__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 (__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) {
@@ -448,7 +519,7 @@ void CPulseAudioClient::finalize() {
     __mIsInit = false;
 }
 
-int CPulseAudioClient::read(void* buffer, size_t length) throw(CAudioError) {
+int CPulseAudioClient::read(void* buffer, size_t length) {
     if (__mIsInit == false) {
         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
     }
@@ -482,14 +553,14 @@ int CPulseAudioClient::read(void* buffer, size_t length) throw(CAudioError) {
 
                 if (__mSyncReadLength <= 0) {
 #ifdef _AUDIO_IO_DEBUG_TIMING_
-                    AUDIO_IO_LOGD("readLength(%d byte) is not valid. wait...", __mSyncReadLength);
+                    AUDIO_IO_LOGD("readLength(%zu 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);
+                        THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pa_stream_drop() : ret[%d]", ret); //LCOV_EXCL_LINE
                     }
                 } else {
                     __mSyncReadIndex = 0;
@@ -504,7 +575,7 @@ int CPulseAudioClient::read(void* buffer, size_t length) throw(CAudioError) {
 
             // 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);
+            AUDIO_IO_LOGD("memcpy() that a peeked buffer[%p], index[%zu], length[%zu] on out buffer", (const uint8_t*)(__mpSyncReadDataPtr) + __mSyncReadIndex, __mSyncReadIndex, l);
 #endif
             memcpy(buffer, (const uint8_t*)__mpSyncReadDataPtr + __mSyncReadIndex, l);
 
@@ -522,7 +593,7 @@ int CPulseAudioClient::read(void* buffer, size_t length) throw(CAudioError) {
 #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);
+                    THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "Failed pa_stream_drop() : ret[%d]", ret); //LCOV_EXCL_LINE
                 }
 
                 // Reset the internal pointer
@@ -534,16 +605,16 @@ int CPulseAudioClient::read(void* buffer, size_t length) throw(CAudioError) {
 
         pa_threaded_mainloop_unlock(__mpMainloop);
         __mIsUsedSyncRead = false;
-    } catch (CAudioError e) {
+    } catch (CAudioError& e) {
         pa_threaded_mainloop_unlock(__mpMainloop);
         __mIsUsedSyncRead = false;
-        throw e;
+        throw;
     }
 
     return length;
 }
 
-int CPulseAudioClient::peek(const void** buffer, size_t* length) throw(CAudioError) {
+int CPulseAudioClient::peek(const void** buffer, size_t* length) {
     if (__mIsInit == false) {
         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
     }
@@ -569,17 +640,17 @@ int CPulseAudioClient::peek(const void** buffer, size_t* length) throw(CAudioErr
     }
 
 #ifdef _AUDIO_IO_DEBUG_TIMING_
-    AUDIO_IO_LOGD("buffer[%p], length[%d]", *buffer, *length);
+    AUDIO_IO_LOGD("buffer[%p], length[%zu]", *buffer, *length);
 #endif
 
     if (ret < 0) {
-        THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_peek() : err[%d]", ret);
+        THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_peek() : err[%d]", ret); //LCOV_EXCL_LINE
     }
 
     return ret;
 }
 
-int CPulseAudioClient::drop() throw(CAudioError) {
+int CPulseAudioClient::drop() {
     if (__mIsInit == false) {
         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
     }
@@ -605,13 +676,13 @@ int CPulseAudioClient::drop() throw(CAudioError) {
     }
 
     if (ret < 0) {
-        THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_drop() : err[%d]", ret);
+        THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_FAILED_OPERATION, "Failed pa_stream_drop() : err[%d]", ret); //LCOV_EXCL_LINE
     }
 
     return ret;
 }
 
-int CPulseAudioClient::write(const void* data, size_t length) throw(CAudioError) {
+int CPulseAudioClient::write(const void* data, size_t length) {
     if (data == NULL) {
         THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_ARGUMENT, "The parameter is invalid");
     }
@@ -623,18 +694,24 @@ int CPulseAudioClient::write(const void* data, size_t length) throw(CAudioError)
     int ret = 0;
 
 #ifdef _AUDIO_IO_DEBUG_TIMING_
-    AUDIO_IO_LOGD("data[%p], length[%d]", data, length);
+    AUDIO_IO_LOGD("data[%p], length[%zu], First[%d]", data, length, __mIsFirstStream);
 #endif
-    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 (isInThread() == false) {
         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 {
+        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;
@@ -647,20 +724,20 @@ int CPulseAudioClient::write(const void* data, size_t length) throw(CAudioError)
                 delete [] dummy;
             }
             __mIsFirstStream = false;
-            AUDIO_IO_LOGW("FIRST STREAM CALLBACK : length[%d], prebuf[%d], dummy[%d]",
+            AUDIO_IO_LOGW("FIRST STREAM CALLBACK : length[%zu], prebuf[%d], dummy[%zu]",
                           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::EError::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); //LCOV_EXCL_LINE
     }
 
     return ret;
 }
 
-void CPulseAudioClient::cork(bool cork) throw(CAudioError) {
+void CPulseAudioClient::cork(bool cork) {
     AUDIO_IO_LOGD("cork[%d]", cork);
 
     if (__mIsInit == false) {
@@ -688,7 +765,7 @@ void CPulseAudioClient::cork(bool cork) throw(CAudioError) {
     return;
 }
 
-bool CPulseAudioClient::isCorked() throw(CAudioError) {
+bool CPulseAudioClient::isCorked() {
     if (__mIsInit == false) {
         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
     }
@@ -705,19 +782,33 @@ bool CPulseAudioClient::isCorked() throw(CAudioError) {
         isCork = pa_stream_is_corked(__mpStream);
     }
 
+#ifdef _AUDIO_IO_DEBUG_TIMING_
     AUDIO_IO_LOGD("isCork[%d]", isCork);
+#endif
     return static_cast<bool>(isCork);
 }
 
-bool CPulseAudioClient::drain() throw(CAudioError) {
+bool CPulseAudioClient::drain() {
     if (__mIsInit == false) {
         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
     }
 
     checkRunningState();
 
-    if (__mIsDraining)
+    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) {
         AUDIO_IO_LOGD("drain");
@@ -729,6 +820,7 @@ bool CPulseAudioClient::drain() throw(CAudioError) {
         }
         pa_operation_unref(o);
         pa_threaded_mainloop_unlock(__mpMainloop);
+        AUDIO_IO_LOGD("drain done");
     } else {
         AUDIO_IO_LOGD("drain in thread");
         pa_operation_unref(pa_stream_drain(__mpStream, __successDrainCbInThread, this));
@@ -738,7 +830,7 @@ bool CPulseAudioClient::drain() throw(CAudioError) {
     return true;
 }
 
-bool CPulseAudioClient::flush() throw(CAudioError) {
+bool CPulseAudioClient::flush() {
     if (__mIsInit == false) {
         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
     }
@@ -758,7 +850,7 @@ bool CPulseAudioClient::flush() throw(CAudioError) {
     return true;
 }
 
-size_t CPulseAudioClient::getWritableSize() throw(CAudioError) {
+size_t CPulseAudioClient::getWritableSize() {
     if (__mIsInit == false) {
         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
     }
@@ -782,7 +874,7 @@ size_t CPulseAudioClient::getWritableSize() throw(CAudioError) {
     return ret;
 }
 
-void CPulseAudioClient::checkRunningState() throw(CAudioError) {
+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);
     }
@@ -798,7 +890,7 @@ void CPulseAudioClient::checkRunningState() throw(CAudioError) {
 #endif
 }
 
-bool CPulseAudioClient::isInThread() throw(CAudioError) {
+bool CPulseAudioClient::isInThread() {
     int ret = pa_threaded_mainloop_in_thread(__mpMainloop);
 
 #ifdef _AUDIO_IO_DEBUG_TIMING_
@@ -807,7 +899,8 @@ bool CPulseAudioClient::isInThread() throw(CAudioError) {
     return static_cast<bool>(ret);
 }
 
-size_t CPulseAudioClient::getReadableSize() throw(CAudioError) {
+//LCOV_EXCL_START
+size_t CPulseAudioClient::getReadableSize() {
     if (__mIsInit == false) {
         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
     }
@@ -831,7 +924,7 @@ size_t CPulseAudioClient::getReadableSize() throw(CAudioError) {
     return ret;
 }
 
-size_t CPulseAudioClient::getBufferSize() throw(CAudioError) {
+size_t CPulseAudioClient::getBufferSize() {
     if (__mIsInit == false) {
         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
     }
@@ -853,16 +946,16 @@ size_t CPulseAudioClient::getBufferSize() throw(CAudioError) {
 
         if (__mDirection == EStreamDirection::STREAM_DIRECTION_PLAYBACK) {
             ret = attr->tlength;
-            AUDIO_IO_LOGD("PLAYBACK buffer size[%d]", ret);
+            AUDIO_IO_LOGD("PLAYBACK buffer size[%zu]", ret);
         } else {
             ret = attr->fragsize;
-            AUDIO_IO_LOGD("RECORD buffer size[%d]", ret);
+            AUDIO_IO_LOGD("RECORD buffer size[%zu]", ret);
         }
-    } catch (CAudioError err) {
+    } catch (CAudioError& e) {
         if (isInThread() == false) {
             pa_threaded_mainloop_unlock(__mpMainloop);
         }
-        throw err;
+        throw;
     }
 
     if (isInThread() == false) {
@@ -872,7 +965,7 @@ size_t CPulseAudioClient::getBufferSize() throw(CAudioError) {
     return ret;
 }
 
-pa_usec_t CPulseAudioClient::getLatency() throw(CAudioError) {
+pa_usec_t CPulseAudioClient::getLatency() {
     if (__mIsInit == false) {
         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
     }
@@ -908,9 +1001,9 @@ pa_usec_t CPulseAudioClient::getLatency() throw(CAudioError) {
             /* Wait until latency data is available again */
             pa_threaded_mainloop_wait(__mpMainloop);
         }
-    } catch (CAudioError e) {
+    } catch (CAudioError& e) {
         pa_threaded_mainloop_unlock(__mpMainloop);
-        throw e;
+        throw;
     }
 
     pa_threaded_mainloop_unlock(__mpMainloop);
@@ -918,7 +1011,7 @@ pa_usec_t CPulseAudioClient::getLatency() throw(CAudioError) {
     return negative ? 0 : ret;
 }
 
-pa_usec_t CPulseAudioClient::getFinalLatency() throw(CAudioError) {
+pa_usec_t CPulseAudioClient::getFinalLatency() {
     if (__mIsInit == false) {
         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
     }
@@ -946,10 +1039,10 @@ pa_usec_t CPulseAudioClient::getFinalLatency() throw(CAudioError) {
 
             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::EError::ERROR_NOT_SUPPORTED, "This version(ver.%d) is not supported", ver);
@@ -958,11 +1051,11 @@ pa_usec_t CPulseAudioClient::getFinalLatency() throw(CAudioError) {
         if (isInThread() == false) {
             pa_threaded_mainloop_unlock(__mpMainloop);
         }
-    } catch (CAudioError e) {
+    } catch (CAudioError& e) {
         if (isInThread() == false) {
             pa_threaded_mainloop_unlock(__mpMainloop);
         }
-        throw e;
+        throw;
     }
 
     return ret;
@@ -975,3 +1068,4 @@ CPulseAudioClient::EStreamDirection CPulseAudioClient::getStreamDirection() {
 CPulseStreamSpec CPulseAudioClient::getStreamSpec() {
     return __mSpec;
 }
+//LCOV_EXCL_STOP