Rearrange internalLock()
[platform/core/api/audio-io.git] / src / cpp / CAudioOutput.cpp
index 5a44bd9..b4aeb8a 100644 (file)
  */
 
 
-#include <vconf.h>
+#include <new>
+
 #include "CAudioIODef.h"
+#include <sched.h>
 
 using namespace std;
 using namespace tizen_media_audio;
@@ -28,6 +30,7 @@ CAudioOutput::CAudioOutput(CAudioInfo& info) :
     CAudioIO(info),
     __mIsUsedSyncWrite(false),
     __mIsInit(false) {
+    mDirection = CAudioInfo::EAudioDirection::AUDIO_DIRECTION_OUT;
 }
 
 CAudioOutput::CAudioOutput(
@@ -37,6 +40,7 @@ CAudioOutput::CAudioOutput(
         CAudioInfo::EAudioType  audioType) :
     __mIsUsedSyncWrite(false),
     __mIsInit(false) {
+    mDirection = CAudioInfo::EAudioDirection::AUDIO_DIRECTION_OUT;
     mAudioInfo = CAudioInfo(sampleRate, channel, sampleType, audioType, -1);
 }
 
@@ -67,15 +71,17 @@ void CAudioOutput::onStream(CPulseAudioClient* pClient, size_t length) {
     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) {
+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);
+    AUDIO_IO_LOGD("[pHandler:%p], [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);
+    AUDIO_IO_LOGD("[pHandler:%p], [signal:%d], [value:%d]", pHandler, signal, value);
     CAudioIO::onSignal(pHandler, signal, value);
 }
 
@@ -91,7 +97,7 @@ bool CAudioOutput::__IsReady() {
     return CAudioIO::IsReady();
 }
 
-void CAudioOutput::initialize() throw(CAudioError) {
+void CAudioOutput::initialize() {
     if (__IsInit() == true) {
         return;
     }
@@ -101,18 +107,18 @@ void CAudioOutput::initialize() throw(CAudioError) {
 
         // 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 (CAudioError& e) {
         finalize();
-        throw err;
+        throw;
+    } catch (const std::bad_alloc&) {
+        finalize();
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed to allocate CAudioSessionHandler object");
     }
 }
 
@@ -130,64 +136,84 @@ void CAudioOutput::finalize() {
     __setInit(false);
 }
 
-void CAudioOutput::prepare() throw(CAudioError) {
+void CAudioOutput::prepare() {
     if (__IsInit() == false) {
         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CAudioOutput");
     }
 
     if (__IsReady() == true) {
         AUDIO_IO_LOGD("Already prepared CAudioOutput");
+        CAudioIO::prepare();
         return;
     }
 
-    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));
+    }
 
+    try {
         if (mpAudioSessionHandler->getId() < 0) {  // Did not registerSound()
-            if (isForceIgnore() == false) {
-                // Register ASM Listener
+            if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSession() == false) {
+                /* Register ASM Listener */
                 AUDIO_IO_LOGD("Register ASM Listener");
                 mpAudioSessionHandler->registerSound();
             }
         }
 
-        // Init StreamSpec
-        AUDIO_IO_LOGD("Set Stream Spec : CPulseStreamSpec::STREAM_LATENCY_OUTPUT_DEFAULT");
+        CAudioIO::setInternalStreamInfo();
+
+        if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSession() == false)
+            mpAudioSessionHandler->updatePlaying();
+    } catch (CAudioError& e) {
+        throw;
+    }
+
+    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
         CPulseStreamSpec spec(streamSpec, mAudioInfo);
 
-        // Create PulseAudio Handler
+        internalLock();
+
+        /* 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
+        /* Initialize PulseAudio Handler */
         mpPulseAudioClient->initialize();
-
-        if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSessionEvent() == false) {
-            /* Updates ASM to PLAYING */
-            mpAudioSessionHandler->updatePlaying();
-        }
-
+#ifndef DISABLE_MOBILE_BACK_COMP
+        /* Uncork stream which is created with CORKED flag */
+        mpPulseAudioClient->cork(false);
+#endif
         internalUnlock();
 
         CAudioIO::prepare();
-    } catch (CAudioError e) {
+    } catch (CAudioError& e) {
+        SAFE_FINALIZE(mpPulseAudioClient);
+        SAFE_DELETE(mpPulseAudioClient);
+        internalUnlock();
+        throw;
+    } catch (const std::bad_alloc&) {
         internalUnlock();
-        throw e;
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed to allocate CPulseAudioClient object");
     }
 }
 
-void CAudioOutput::unprepare() throw(CAudioError) {
+void CAudioOutput::unprepare() {
     if (__IsInit() == false) {
-        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CAudioOutput");
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
+                        "Did not initialize CAudioOutput");
     }
 
     if (__IsReady() == false) {
@@ -196,6 +222,14 @@ void CAudioOutput::unprepare() throw(CAudioError) {
     }
 
     try {
+        if (mpAudioSessionHandler->getId() >= 0 && !mIsInterrupted) {
+            if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSession() == false) {
+                CPulseStreamSpec::EStreamLatency streamSpec;
+                streamSpec = mpPulseAudioClient->getStreamSpec().getStreamLatency();
+                if (CPulseStreamSpec::EStreamLatency::STREAM_LATENCY_OUTPUT_DEFAULT_ASYNC != streamSpec)
+                    CAudioIO::drain();
+            }
+        }
         CAudioIO::unprepare();
 
         internalLock();
@@ -203,30 +237,29 @@ void CAudioOutput::unprepare() throw(CAudioError) {
         SAFE_FINALIZE(mpPulseAudioClient);
         SAFE_DELETE(mpPulseAudioClient);
 
-        if (mpAudioSessionHandler->getId() >= 0) {
-            /* Updates ASM to STOP */
-            if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSessionEvent() == false) {
-                mpAudioSessionHandler->updateStop();
-            }
-
-            bool isSkip = mpAudioSessionHandler->isSkipSessionEvent();
-            if (isSkip == false) {
-                mpAudioSessionHandler->unregisterSound();
-            }
-        }
+        internalUnlock();
 
+    } catch (CAudioError& e) {
         internalUnlock();
+        throw;
+    }
 
+    try {
+        if (mpAudioSessionHandler->getId() >= 0) {
+            if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSession() == false)
+                mpAudioSessionHandler->updateStop();
+        }
         CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE);
-    } catch (CAudioError e) {
+    } catch (CAudioError& e) {
         internalUnlock();
-        throw e;
+        throw;
     }
 }
 
-void CAudioOutput::pause() throw(CAudioError) {
+void CAudioOutput::pause() {
     if (__IsInit() == false || __IsReady() == false) {
-        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioOutput");
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
+                        "Did not initialize or prepare CAudioOutput");
     }
 
     if (CAudioIO::getState() != CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING) {
@@ -241,25 +274,16 @@ void CAudioOutput::pause() throw(CAudioError) {
     try {
         CAudioIO::pause();
 
-        internalLock();
-
-        /* 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;
+    } catch (CAudioError& e) {
+        throw;
     }
 }
 
-void CAudioOutput::resume() throw(CAudioError) {
+void CAudioOutput::resume() {
     if (__IsInit() == false || __IsReady() == false) {
-        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioOutput");
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
+                        "Did not initialize or prepare CAudioOutput");
     }
 
     if (CAudioIO::getState() != CAudioInfo::EAudioIOState::AUDIO_IO_STATE_PAUSED) {
@@ -272,64 +296,65 @@ void CAudioOutput::resume() throw(CAudioError) {
     }
 
     try {
-        internalLock();
-
-        if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSessionEvent() == false) {
-            /* Updates ASM to PLAYING */
-            mpAudioSessionHandler->updatePlaying();
-        }
-
-        internalUnlock();
-
         CAudioIO::resume();
 
         CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_RUNNING);
-    } catch (CAudioError e) {
-        internalUnlock();
-        throw e;
+    } catch (CAudioError& e) {
+        throw;
     }
 }
 
-void CAudioOutput::drain() throw(CAudioError) {
+void CAudioOutput::drain() {
+    CPulseStreamSpec::EStreamLatency streamSpec;
+
     if (__IsInit() == false || __IsReady() == false) {
-        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioOutput");
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
+                        "Did not initialize or prepare CAudioOutput");
     }
 
+    streamSpec = mpPulseAudioClient->getStreamSpec().getStreamLatency();
+    if (CPulseStreamSpec::EStreamLatency::STREAM_LATENCY_OUTPUT_DEFAULT_ASYNC == streamSpec)
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_OPERATION, "async type don't support drain");
+
     try {
         CAudioIO::drain();
-    } catch (CAudioError e) {
-        throw e;
+    } catch (CAudioError& e) {
+        throw;
     }
 }
 
-void CAudioOutput::flush() throw(CAudioError) {
+void CAudioOutput::flush() {
     if (__IsInit() == false || __IsReady() == false) {
-        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioOutput");
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
+                        "Did not initialize or prepare CAudioOutput");
     }
 
     try {
         CAudioIO::flush();
-    } catch (CAudioError e) {
-        throw e;
+    } catch (CAudioError& e) {
+        throw;
     }
 }
 
-int CAudioOutput::getBufferSize() throw(CAudioError) {
+int CAudioOutput::getBufferSize() {
     if (__IsInit() == false) {
-        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CAudioOutput");
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
+                        "Did not initialize or prepare CAudioOutput");
     }
 
     /* FIXME : return calculated size here to satisfy backward compatibility */
     return (mAudioInfo.getSampleRate() * DEFAULT_PERIOD_SIZE) / 1000 * mAudioInfo.getSampleSize();
 }
 
-size_t CAudioOutput::write(const void* buffer, size_t length) throw(CAudioError) {
+size_t CAudioOutput::write(const void* buffer, size_t length) {
     if (__IsInit() == false || __IsReady() == false) {
-        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize or prepare CAudioOutput");
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
+                        "Did not initialize or prepare CAudioOutput");
     }
 
     if (buffer == NULL) {
-        THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "Parameters are invalid - buffer:%p, length:%zu", buffer, length);
+        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,
@@ -337,10 +362,11 @@ size_t CAudioOutput::write(const void* buffer, size_t length) throw(CAudioError)
     }
 
     /* When write() is called in PulseAudio callback, bypass a pcm data to CPulseAudioClient (For Asynchronous) */
-    if (mpPulseAudioClient->isInThread() == true) {
+    if (mpPulseAudioClient && mpPulseAudioClient->isInThread() == true) {
         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);
+            THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION,
+                                   "The written result is invalid ret:%d", ret);
         }
 
 #ifdef _AUDIO_IO_DEBUG_TIMING_
@@ -353,10 +379,14 @@ size_t CAudioOutput::write(const void* buffer, size_t length) throw(CAudioError)
     try {
         /* For synchronization */
         internalLock();
+        if (mIsInterrupted) {
+            THROW_ERROR_MSG(CAudioError::EError::ERROR_POLICY_BLOCKED, "audio io is interrupted");
+        }
 
         // If another thread did call unprepare, do not write
         if (mpPulseAudioClient == NULL)
-            THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CPulseAudioClient");
+            THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED,
+                            "Did not initialize CPulseAudioClient");
 
         // Sets synchronous flag
         __mIsUsedSyncWrite = true;
@@ -383,21 +413,21 @@ size_t CAudioOutput::write(const void* buffer, size_t length) throw(CAudioError)
 
             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);
+                THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INTERNAL_OPERATION,
+                                       "The written result is invalid ret:%d", ret);
             }
 
             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
+        sched_yield();
+    } catch (CAudioError& e) {
         __mIsUsedSyncWrite = false;
         internalUnlock();
-        throw e;
+        throw;
     }
 
     return length;