Add writable-size wait timeout log / Terminate write operation with accumulated timeouts
[platform/core/api/audio-io.git] / src / cpp / CAudioOutput.cpp
index e5fc08b..d624c39 100644 (file)
  */
 
 
-#include <vconf.h>
+#include <new>
+
 #include "CAudioIODef.h"
+#include <sched.h>
+#include <chrono>
 
 using namespace std;
 using namespace tizen_media_audio;
 
+static constexpr auto cond_wait_ms = 200ms;
+static constexpr auto writable_timeout_s = 5s;
 
 /**
  * class CAudioOutput
  */
+
 CAudioOutput::CAudioOutput(CAudioInfo& info) :
     CAudioIO(info),
-    __mIsUsedSyncWrite(false) {
-}
-
-CAudioOutput::CAudioOutput(
-        unsigned int            sampleRate,
-        CAudioInfo::EChannel    channel,
-        CAudioInfo::ESampleType sampleType,
-        CAudioInfo::EAudioType  audioType) :
-    __mIsUsedSyncWrite(false) {
-    mAudioInfo = CAudioInfo(sampleRate, channel, sampleType, audioType, -1);
-}
-
-CAudioOutput::~CAudioOutput() {
+    __mIsUsedSyncWrite(false),
+    __mIsInit(false) {
+    mDirection = CAudioInfo::EAudioDirection::AUDIO_DIRECTION_OUT;
 }
 
 void CAudioOutput::onStream(CPulseAudioClient* pClient, size_t length) {
@@ -49,11 +45,14 @@ void CAudioOutput::onStream(CPulseAudioClient* pClient, size_t length) {
      * Does not call CAudioIO::onStream() for synchronization
      * if a user is using write()
      */
-    if (__mIsUsedSyncWrite == true) {
+    if (__mIsUsedSyncWrite) {
 #ifdef _AUDIO_IO_DEBUG_TIMING_
-        AUDIO_IO_LOGD("Sync Write Mode! - signal! - pClient:[%p], length:[%d]", pClient, length);
+        AUDIO_IO_LOGD("Sync Write Mode! - signal! - pClient:[%p], length:[%zu]", pClient, length);
 #endif
-        internalSignal();
+        {
+            std::lock_guard<std::mutex> cond_guard(mCondMutex);
+        }
+        mCond.notify_one();
         return;
     }
 
@@ -61,324 +60,279 @@ void CAudioOutput::onStream(CPulseAudioClient* pClient, size_t length) {
      * Accrues callback function
      */
 #ifdef _AUDIO_IO_DEBUG_TIMING_
-    AUDIO_IO_LOGD("pClient:[%p], length:[%d]", pClient, length);
+    AUDIO_IO_LOGD("pClient:[%p], length:[%zu]", pClient, length);
 #endif
     CAudioIO::onStream(pClient, length);
 }
 
-void CAudioOutput::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:0x%x], [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 CAudioOutput::onSignal(CAudioSessionHandler* pHandler, mm_sound_signal_name_t signal, int value) {
-    assert(pHandler);
-    AUDIO_IO_LOGD("[pHandler:0x%x], [signal:%d], [value:%d]", pHandler, signal, value);
-    CAudioIO::onSignal(pHandler, signal, value);
-}
-
-void CAudioOutput::__setInit(bool flag) {
+void CAudioOutput::__setInit(bool flag) noexcept {
     __mIsInit = flag;
 }
 
-bool CAudioOutput::__IsInit() {
-    return (CAudioIO::isInit() == true && __mIsInit == true);
+bool CAudioOutput::__IsInit() noexcept {
+    return (CAudioIO::isInit() && __mIsInit);
 }
 
-bool CAudioOutput::__IsReady() {
+bool CAudioOutput::__IsReady() noexcept {
     return CAudioIO::IsReady();
 }
 
-void CAudioOutput::initialize() throw (CAudioError) {
-    if (__IsInit() == true) {
+void CAudioOutput::initialize() {
+    if (__IsInit())
         return;
-    }
 
     try {
         CAudioIO::initialize();
-
-        // Create ASM Handler
-        mpAudioSessionHandler = new CAudioSessionHandler(CAudioSessionHandler::EAudioSessionType::AUDIO_SESSION_TYPE_PLAYBACK, mAudioInfo, this);
-        if (mpAudioSessionHandler == NULL) {
-            THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed to allocate CAudioSessionHandler object");
-        }
-
-        // Initialize ASM Handler
-        mpAudioSessionHandler->initialize();
-
         __setInit(true);
-        CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE);
-    } catch (CAudioError err) {
+    } catch (const CAudioError& e) {
+//LCOV_EXCL_START
         finalize();
-        throw err;
+        throw;
+//LCOV_EXCL_STOP
     }
+
+    CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE);
 }
 
 void CAudioOutput::finalize() {
-    if (__IsInit() == false) {
+    if (!__IsInit()) {
         AUDIO_IO_LOGD("Did not initialize");
         return;
     }
 
-    SAFE_FINALIZE(mpAudioSessionHandler);
-    SAFE_DELETE(mpAudioSessionHandler);
-
     CAudioIO::finalize();
-
     __setInit(false);
 }
 
-void CAudioOutput::prepare() throw (CAudioError) {
-    if (__IsInit() == false) {
-        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CAudioOutput");
-    }
+void CAudioOutput::prepare() {
+    if (!__IsInit())
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CAudioOutput"); //LCOV_EXCL_LINE
 
-    if (__IsReady() == true) {
+    if (__IsReady()) {
+//LCOV_EXCL_START
         AUDIO_IO_LOGD("Already prepared CAudioOutput");
+        CAudioIO::prepare();
         return;
+//LCOV_EXCL_STOP
     }
 
-    try {
-        internalLock();
-
-        // Check to invalid AudioType
-        CAudioInfo::EAudioType audioType = mAudioInfo.getAudioType();
-        if (audioType < CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA || audioType >= CAudioInfo::EAudioType::AUDIO_TYPE_MAX) {
-            THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "The audioType is invalid [type:%d]", static_cast<int>(audioType));
-        }
+    /* Check invalid AudioType */
+    CAudioInfo::EAudioType audioType = mAudioInfo.getAudioType();
+    if (audioType < CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA ||
+        audioType >= CAudioInfo::EAudioType::AUDIO_TYPE_MAX)
+        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()
-            if (isForceIgnore() == false) {
-                // Register ASM Listener
-                AUDIO_IO_LOGD("Register ASM Listener");
-                mpAudioSessionHandler->registerSound();
-            }
+    try {
+        /* Init StreamSpec */
+        CPulseStreamSpec::EStreamLatency streamSpec = CPulseStreamSpec::EStreamLatency::STREAM_LATENCY_OUTPUT_DEFAULT;
+#ifndef DISABLE_MOBILE_BACK_COMP
+        if (!mStreamCallback.onStream) {
+            AUDIO_IO_LOGD("Set Stream Spec : CPulseStreamSpec::STREAM_LATENCY_OUTPUT_DEFAULT");
+            streamSpec = CPulseStreamSpec::EStreamLatency::STREAM_LATENCY_OUTPUT_DEFAULT;
+        } else {
+            AUDIO_IO_LOGD("Set Stream Spec : CPulseStreamSpec::STREAM_LATENCY_OUTPUT_DEFAULT_ASYNC");
+            streamSpec = CPulseStreamSpec::EStreamLatency::STREAM_LATENCY_OUTPUT_DEFAULT_ASYNC;
         }
+#endif
+        /* Override the default value by audio type */
+        if (audioType == CAudioInfo::EAudioType::AUDIO_OUT_TYPE_VOIP)
+            streamSpec = CPulseStreamSpec::EStreamLatency::STREAM_LATENCY_OUTPUT_VOIP;
+        else if (audioType == CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA_NETWORK_SOURCE)
+            streamSpec = CPulseStreamSpec::EStreamLatency::STREAM_LATENCY_OUTPUT_HIGH;
 
-        // Init StreamSpec
-        AUDIO_IO_LOGD("Set Stream Spec : CPulseStreamSpec::STREAM_LATENCY_OUTPUT_MID");
-        CPulseStreamSpec::EStreamLatency streamSpec = CPulseStreamSpec::EStreamLatency::STREAM_LATENCY_OUTPUT_MID;
         CPulseStreamSpec spec(streamSpec, mAudioInfo);
 
-        // Create PulseAudio Handler
-        mpPulseAudioClient = new CPulseAudioClient(CPulseAudioClient::EStreamDirection::STREAM_DIRECTION_PLAYBACK, spec, this);
-        if (mpPulseAudioClient == NULL) {
-            THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed to allocate CPulseAudioClient object");
-        }
-
-        // Initialize PulseAudio Handler
+        std::unique_lock<std::mutex> mutex(mMutex);
+        mpPulseAudioClient = new CPulseAudioClient(CPulseAudioClient::EStreamDirection::STREAM_DIRECTION_PLAYBACK,
+                                                   spec, this);
         mpPulseAudioClient->initialize();
-
-        if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSessionEvent() == false) {
-            /* Updates ASM to PLAYING */
-            mpAudioSessionHandler->updatePlaying();
-        }
-
-        internalUnlock();
+#ifndef DISABLE_MOBILE_BACK_COMP
+        /* Uncork stream which is created with CORKED flag */
+        mpPulseAudioClient->cork(false);
+#endif
+        mutex.unlock();
 
         CAudioIO::prepare();
-    } catch (CAudioError e) {
-        internalUnlock();
-        throw e;
+    } catch (const CAudioError& e) {
+//LCOV_EXCL_START
+        SAFE_FINALIZE(mpPulseAudioClient);
+        SAFE_DELETE(mpPulseAudioClient);
+        throw;
+//LCOV_EXCL_STOP
+    } catch (const std::bad_alloc&) {
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed to allocate CPulseAudioClient object");
     }
 }
 
-void CAudioOutput::unprepare() throw (CAudioError) {
-    if (__IsInit() == false) {
-        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CAudioOutput");
-    }
+void CAudioOutput::unprepare() {
+    if (!__IsInit())
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, //LCOV_EXCL_LINE
+                        "Did not initialize CAudioOutput");         //LCOV_EXCL_LINE
 
-    if (__IsReady() == false) {
+    if (!__IsReady()) {
         AUDIO_IO_LOGD("Already unprepared");
         return;
     }
 
-    try {
-        CAudioIO::unprepare();
+    CAudioIO::unprepare();
 
-        internalLock();
+    std::unique_lock<std::mutex> mutex(mMutex);
+    if (mpPulseAudioClient && mpPulseAudioClient->isInThread())
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "Can't unprepare inside pulseaudio thread");
+    SAFE_FINALIZE(mpPulseAudioClient);
+    SAFE_DELETE(mpPulseAudioClient);
+    mutex.unlock();
 
-        SAFE_FINALIZE(mpPulseAudioClient);
-        SAFE_DELETE(mpPulseAudioClient);
+    CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE);
+}
 
-        if (mpAudioSessionHandler->getId() >= 0) {
-            /* Updates ASM to STOP */
-            if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSessionEvent() == false) {
-                mpAudioSessionHandler->updateStop();
-            }
+void CAudioOutput::pause() {
+    if (!__IsInit() || !__IsReady())
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,    //LCOV_EXCL_LINE
+                        "Did not initialize or prepare CAudioOutput"); //LCOV_EXCL_LINE
 
-            bool isSkip = mpAudioSessionHandler->isSkipSessionEvent();
-            if (isSkip == false) {
-                mpAudioSessionHandler->unregisterSound();
-            }
-        }
+    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");
 
-        internalUnlock();
+    if (mpPulseAudioClient->isInThread() )
+        THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_OPERATION, "Can't pause in thread"); //LCOV_EXCL_LINE
 
-        CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE);
-    } catch (CAudioError e) {
-        internalUnlock();
-        throw e;
-    }
+    CAudioIO::pause();
+    CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED);
 }
 
-void CAudioOutput::pause() throw (CAudioError) {
-    if (__IsInit() == false || __IsReady() == false) {
-        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioOutput");
-    }
+void CAudioOutput::resume() {
+    if (!__IsInit() || !__IsReady())
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,    //LCOV_EXCL_LINE
+                        "Did not initialize or prepare CAudioOutput"); //LCOV_EXCL_LINE
 
-    try {
-        CAudioIO::pause();
+    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");
 
-        internalLock();
+    if (mpPulseAudioClient->isInThread())
+        THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_OPERATION, "Can't resume in thread"); //LCOV_EXCL_LINE
 
-        /* Updates ASM to STOP */
-        if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSessionEvent() == false) {
-            mpAudioSessionHandler->updateStop();
-        }
-
-        internalUnlock();
-
-        CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED);
-    } catch (CAudioError e) {
-        internalUnlock();
-        throw e;
-    }
+    CAudioIO::resume();
+    CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING);
 }
 
-void CAudioOutput::resume() throw (CAudioError) {
-    if (__IsInit() == false || __IsReady() == false) {
-        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioOutput");
-    }
+void CAudioOutput::drain() {
+    if (!__IsInit() || !__IsReady())
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,    //LCOV_EXCL_LINE
+                        "Did not initialize or prepare CAudioOutput"); //LCOV_EXCL_LINE
 
-    try {
-        internalLock();
+    if (mStreamCallback.onStream)
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "async type don't support drain");
 
-        if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSessionEvent() == false) {
+    std::unique_lock<std::mutex> defer_mutex(mMutex, std::defer_lock);
+    if (!mpPulseAudioClient->isInThread())
+        defer_mutex.lock();
 
-            /* Updates ASM to PLAYING */
-            mpAudioSessionHandler->updatePlaying();
-        }
-
-        internalUnlock();
-
-        CAudioIO::resume();
-
-        CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING);
-    } catch (CAudioError e) {
-        internalUnlock();
-        throw e;
-    }
+    mpPulseAudioClient->drain();
 }
 
-void CAudioOutput::drain() throw (CAudioError) {
-    if (__IsInit() == false || __IsReady() == false) {
-        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioOutput");
-    }
+void CAudioOutput::flush() {
+    if (!__IsInit() || !__IsReady())
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,    //LCOV_EXCL_LINE
+                        "Did not initialize or prepare CAudioOutput"); //LCOV_EXCL_LINE
 
-    try {
-        CAudioIO::drain();
-    } catch (CAudioError e) {
-        throw e;
-    }
+    CAudioIO::flush();
 }
 
-void CAudioOutput::flush() throw (CAudioError) {
-    if (__IsInit() == false || __IsReady() == false) {
-        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioOutput");
-    }
+int CAudioOutput::getBufferSize() {
+    if (!__IsInit())
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,    //LCOV_EXCL_LINE
+                        "Did not initialize or prepare CAudioOutput"); //LCOV_EXCL_LINE
 
-    try {
-        CAudioIO::flush();
-    } catch (CAudioError e) {
-        throw e;
-    }
+    /* FIXME : return calculated size here to satisfy backward compatibility */
+    return (mAudioInfo.getSampleRate() * DEFAULT_PERIOD_SIZE) / 1000 * mAudioInfo.getSampleSize();
 }
 
-int CAudioOutput::getBufferSize() throw (CAudioError) {
-    if (__IsInit() == false) {
-        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CAudioOutput");
-    }
-
-    if (__IsReady() == false) {
-        AUDIO_IO_LOGD("Warning: Did not prepare CAudioOutput, then return zero");
-        return 0;
-    }
-
-    int size = 0;
-
-    try {
-        size = mpPulseAudioClient->getBufferSize();
-    } catch (CAudioError err) {
-        throw err;
-    }
-
-    return size;
-}
+size_t CAudioOutput::write(const void* buffer, size_t length) {
+    if (!__IsInit() || !__IsReady())
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,    //LCOV_EXCL_LINE
+                        "Did not initialize or prepare CAudioOutput"); //LCOV_EXCL_LINE
 
-size_t CAudioOutput::write(const void* buffer, size_t length) throw (CAudioError) {
-    if (__IsInit() == false || __IsReady() == false) {
-        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioOutput");
-    }
+    if (!buffer)
+        THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
+                               "Parameters are invalid - buffer:%p, length:%zu", buffer, length);
 
-    if (buffer == NULL) {
-        THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are invalid - buffer:%p, length:%zu", buffer, length);
-    }
+    if (CAudioIO::getState() != CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING)
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION,
+                        "Can't write if not in Running state");
 
-    /* When write() is called in PulseAudio callback, bypass a pcm data to PulseAudioClient (For Asynchronous) */
-    if (mpPulseAudioClient->isInThread() == true) {
+    /* When write() is called in PulseAudio callback, bypass a pcm data to CPulseAudioClient (For Asynchronous) */
+    if (mpPulseAudioClient && mpPulseAudioClient->isInThread()) {
         int ret = mpPulseAudioClient->write(buffer, length);
-        if (ret < 0) {
-            THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "The written result is invalid ret:%d", ret);
-        }
+        if (ret < 0)
+            THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION,
+                                   "The written result is invalid ret:%d", ret);
+#ifdef _AUDIO_IO_DEBUG_TIMING_
+        AUDIO_IO_LOGD("CPulseAudioClient->write(buffer:%p, length:%zu)", buffer, length);
+#endif
         return length;
     }
 
     try {
         /* For synchronization */
-        internalLock();
+        std::unique_lock<std::mutex> mutex(mMutex);
+
+        // If another thread did call unprepare, do not write
+        if (!mpPulseAudioClient)
+            THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, //LCOV_EXCL_LINE
+                            "Did not initialize CPulseAudioClient");    //LCOV_EXCL_LINE
 
         // Sets synchronous flag
         __mIsUsedSyncWrite = true;
-
         size_t lengthIter = length;
 
         while (lengthIter > 0) {
             size_t l;
+            unsigned int timeouts = 0;
 
             while ((l = mpPulseAudioClient->getWritableSize()) == 0) {
 #ifdef _AUDIO_IO_DEBUG_TIMING_
-                AUDIO_IO_LOGD("writableSize is [%d].. wait", l);
+                AUDIO_IO_LOGD("writableSize is [%zu].. wait", l);
 #endif
-                internalWait();
+                std::unique_lock<std::mutex> cond_mutex(mCondMutex);
+                if (mCond.wait_for(cond_mutex, cond_wait_ms) == std::cv_status::timeout) {
+//LCOV_EXCL_START
+                    AUDIO_IO_LOGW("[%2u] timeout expired for waiting %lld ms of write available...", ++timeouts,
+                                  std::chrono::duration_cast<std::chrono::milliseconds>(cond_wait_ms).count());
+                    if ((cond_wait_ms * timeouts) > writable_timeout_s)
+                        THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION,
+                                               "writable size is 0 for more than %lld seconds...",
+                                               std::chrono::duration_cast<std::chrono::seconds>(writable_timeout_s).count());
+//LCOV_EXCL_STOP
+                }
             }
 
-            if (l > lengthIter) {
+            if (l > lengthIter)
                 l = lengthIter;
-            }
 
 #ifdef _AUDIO_IO_DEBUG_TIMING_
-            AUDIO_IO_LOGD("PulseAudioClient->write(buffer:%p, length:%d)", buffer, l);
+            AUDIO_IO_LOGD("CPulseAudioClient->write(buffer:%p, length:%zu)", buffer, l);
 #endif
 
             int ret = mpPulseAudioClient->write(buffer, l);
-            if (ret < 0) {
-                THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION, "The written result is invalid ret:%d", ret);
-            }
+            if (ret < 0)
+                THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION,//LCOV_EXCL_LINE
+                                       "The written result is invalid ret:%d", ret); //LCOV_EXCL_LINE
 
             buffer = static_cast<const uint8_t*>(buffer) + l;
             lengthIter -= l;
         }  // End of while (length > 0)
 
-        // Unsets synchronous flag
         __mIsUsedSyncWrite = false;
-        internalUnlock();
-    } catch (CAudioError e) {
-        // Unsets synchronous flag
+        mutex.unlock();
+
+        sched_yield();
+    } catch (const CAudioError& e) {
         __mIsUsedSyncWrite = false;
-        internalUnlock();
-        throw e;
+        throw;
     }
 
     return length;