Add pa_threaded_mainloop_lock() in CPulseAudioClient::write()
[platform/core/api/audio-io.git] / src / cpp / CPulseAudioClient.cpp
index bd123bb..7f41a61 100644 (file)
@@ -15,7 +15,6 @@
  */
 
 
-#include <mm.h>
 #include "CAudioIODef.h"
 #include <unistd.h>
 #include <inttypes.h>
@@ -52,7 +51,8 @@ CPulseAudioClient::CPulseAudioClient(
     __mSyncReadLength(0),
     __mIsUsedSyncRead(false),
     __mIsFirstStream(false),
-    __mIsDraining(false) {
+    __mIsDraining(false),
+    __mIsStarted(false) {
 }
 
 CPulseAudioClient::~CPulseAudioClient() {
@@ -223,6 +223,8 @@ void CPulseAudioClient::__streamStartedCb(pa_stream* s, void* 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) {
@@ -232,6 +234,8 @@ void CPulseAudioClient::__streamUnderflowCb(pa_stream* s, void* 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) {
@@ -379,7 +383,7 @@ void CPulseAudioClient::initialize() {
         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
 
@@ -446,22 +450,21 @@ void CPulseAudioClient::initialize() {
 
 void CPulseAudioClient::finalize() {
     AUDIO_IO_LOGD("");
-    if (__mIsInit == false) {
-        return;
-    }
 
-    if (isInThread() == false)
+    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 (isInThread() == false)
+    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 */
@@ -680,16 +683,22 @@ int CPulseAudioClient::write(const void* data, size_t length) {
 #ifdef _AUDIO_IO_DEBUG_TIMING_
     AUDIO_IO_LOGD("data[%p], length[%d], 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;
@@ -783,6 +792,11 @@ bool CPulseAudioClient::drain() {
         return true;
     }
 
+    if (!__mIsStarted) {
+        AUDIO_IO_LOGW("stream not started yet...skip drain");
+        return true;
+    }
+
     if (isInThread() == false) {
         AUDIO_IO_LOGD("drain");
         pa_threaded_mainloop_lock(__mpMainloop);
@@ -793,6 +807,7 @@ bool CPulseAudioClient::drain() {
         }
         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));