Revise cpp codes (mutex/cond)
[platform/core/api/audio-io.git] / src / cpp / CAudioOutput.cpp
index 3f7b79a..a3ca983 100644 (file)
 
 #include "CAudioIODef.h"
 #include <sched.h>
+#include <chrono>
 
 using namespace std;
 using namespace tizen_media_audio;
 
+static constexpr auto cond_wait_ms = 200ms;
+
 /**
  * class CAudioOutput
  */
@@ -45,7 +48,10 @@ void CAudioOutput::onStream(CPulseAudioClient* pClient, size_t length) {
 #ifdef _AUDIO_IO_DEBUG_TIMING_
         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;
     }
 
@@ -94,7 +100,6 @@ void CAudioOutput::finalize() {
     }
 
     CAudioIO::finalize();
-
     __setInit(false);
 }
 
@@ -137,28 +142,25 @@ void CAudioOutput::prepare() {
 
         CPulseStreamSpec spec(streamSpec, mAudioInfo);
 
-        internalLock();
-        mpPulseAudioClient = new CPulseAudioClient(CPulseAudioClient::EStreamDirection::STREAM_DIRECTION_PLAYBACK, spec, this);
+        std::unique_lock<std::mutex> mutex(mMutex);
+        mpPulseAudioClient = new CPulseAudioClient(CPulseAudioClient::EStreamDirection::STREAM_DIRECTION_PLAYBACK,
+                                                   spec, this);
         mpPulseAudioClient->initialize();
 #ifndef DISABLE_MOBILE_BACK_COMP
         /* Uncork stream which is created with CORKED flag */
         mpPulseAudioClient->cork(false);
 #endif
-        internalUnlock();
+        mutex.unlock();
 
         CAudioIO::prepare();
     } catch (const CAudioError& e) {
 //LCOV_EXCL_START
         SAFE_FINALIZE(mpPulseAudioClient);
         SAFE_DELETE(mpPulseAudioClient);
-        internalUnlock();
         throw;
 //LCOV_EXCL_STOP
     } catch (const std::bad_alloc&) {
-//LCOV_EXCL_START
-        internalUnlock();
         THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed to allocate CPulseAudioClient object");
-//LCOV_EXCL_STOP
     }
 }
 
@@ -174,19 +176,12 @@ void CAudioOutput::unprepare() {
 
     CAudioIO::unprepare();
 
-    try {
-        internalLock();
-        if (mpPulseAudioClient && mpPulseAudioClient->isInThread())
-            THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "Can't unprepare inside pulseaudio thread");
-        SAFE_FINALIZE(mpPulseAudioClient);
-        SAFE_DELETE(mpPulseAudioClient);
-        internalUnlock();
-    } catch (const CAudioError& e) {
-//LCOV_EXCL_START
-        internalUnlock();
-        throw;
-//LCOV_EXCL_STOP
-    }
+    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();
 
     CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE);
 }
@@ -231,19 +226,11 @@ void CAudioOutput::drain() {
     if (mStreamCallback.onStream)
         THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "async type don't support drain");
 
-    try {
-        if (mpPulseAudioClient->isInThread()) {
-            mpPulseAudioClient->drain();
-        } else {
-            internalLock();
-            mpPulseAudioClient->drain();
-            internalUnlock();
-        }
-    } catch (const CAudioError& e) {
-        if (!mpPulseAudioClient->isInThread())
-            internalUnlock();
-        throw;
-    }
+    std::unique_lock<std::mutex> defer_mutex(mMutex, std::defer_lock);
+    if (!mpPulseAudioClient->isInThread())
+        defer_mutex.lock();
+
+    mpPulseAudioClient->drain();
 }
 
 void CAudioOutput::flush() {
@@ -290,7 +277,7 @@ size_t CAudioOutput::write(const void* buffer, size_t length) {
 
     try {
         /* For synchronization */
-        internalLock();
+        std::unique_lock<std::mutex> mutex(mMutex);
 
         // If another thread did call unprepare, do not write
         if (!mpPulseAudioClient)
@@ -299,7 +286,6 @@ size_t CAudioOutput::write(const void* buffer, size_t length) {
 
         // Sets synchronous flag
         __mIsUsedSyncWrite = true;
-
         size_t lengthIter = length;
 
         while (lengthIter > 0) {
@@ -309,7 +295,8 @@ size_t CAudioOutput::write(const void* buffer, size_t length) {
 #ifdef _AUDIO_IO_DEBUG_TIMING_
                 AUDIO_IO_LOGD("writableSize is [%zu].. wait", l);
 #endif
-                internalWait();
+                std::unique_lock<std::mutex> cond_mutex(mCondMutex);
+                mCond.wait_for(cond_mutex, cond_wait_ms);
             }
 
             if (l > lengthIter)
@@ -329,12 +316,11 @@ size_t CAudioOutput::write(const void* buffer, size_t length) {
         }  // End of while (length > 0)
 
         __mIsUsedSyncWrite = false;
-        internalUnlock();
+        mutex.unlock();
 
         sched_yield();
     } catch (const CAudioError& e) {
         __mIsUsedSyncWrite = false;
-        internalUnlock();
         throw;
     }