Write dummy write if stream is not written at first callback during prepare situtaion
[platform/core/api/audio-io.git] / src / cpp / CAudioOutput.cpp
index e0524f6..2e5db0e 100644 (file)
  */
 
 
-#include <vconf.h>
 #include "CAudioIODef.h"
+#include <sched.h>
 
 using namespace std;
 using namespace tizen_media_audio;
 
-
 /**
  * class CAudioOutput
  */
@@ -29,6 +28,7 @@ CAudioOutput::CAudioOutput(CAudioInfo& info) :
     CAudioIO(info),
     __mIsUsedSyncWrite(false),
     __mIsInit(false) {
+    mDirection = CAudioInfo::EAudioDirection::AUDIO_DIRECTION_OUT;
 }
 
 CAudioOutput::CAudioOutput(
@@ -38,6 +38,7 @@ CAudioOutput::CAudioOutput(
         CAudioInfo::EAudioType  audioType) :
     __mIsUsedSyncWrite(false),
     __mIsInit(false) {
+    mDirection = CAudioInfo::EAudioDirection::AUDIO_DIRECTION_OUT;
     mAudioInfo = CAudioInfo(sampleRate, channel, sampleType, audioType, -1);
 }
 
@@ -68,9 +69,11 @@ 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: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);
 }
 
@@ -103,7 +106,8 @@ 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");
+            THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY,
+                            "Failed to allocate CAudioSessionHandler object");
         }
 
         // Initialize ASM Handler
@@ -147,17 +151,20 @@ void CAudioOutput::prepare() throw(CAudioError) {
         // 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));
+            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) {
+            if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSession() == false) {
                 // Register ASM Listener
                 AUDIO_IO_LOGD("Register ASM Listener");
                 mpAudioSessionHandler->registerSound();
             }
         }
 
+        CAudioIO::setInternalStreamInfo();
+
         // Init StreamSpec
         AUDIO_IO_LOGD("Set Stream Spec : CPulseStreamSpec::STREAM_LATENCY_OUTPUT_DEFAULT");
         CPulseStreamSpec::EStreamLatency streamSpec = CPulseStreamSpec::EStreamLatency::STREAM_LATENCY_OUTPUT_DEFAULT;
@@ -166,16 +173,15 @@ void CAudioOutput::prepare() throw(CAudioError) {
         // 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");
+            THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY,
+                            "Failed to allocate CPulseAudioClient object");
         }
 
         // Initialize PulseAudio Handler
         mpPulseAudioClient->initialize();
 
-        if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSessionEvent() == false) {
-            /* Updates ASM to PLAYING */
+        if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSession() == false)
             mpAudioSessionHandler->updatePlaying();
-        }
 
         internalUnlock();
 
@@ -188,7 +194,8 @@ void CAudioOutput::prepare() throw(CAudioError) {
 
 void CAudioOutput::unprepare() throw(CAudioError) {
     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) {
@@ -204,20 +211,16 @@ void CAudioOutput::unprepare() throw(CAudioError) {
         SAFE_FINALIZE(mpPulseAudioClient);
         SAFE_DELETE(mpPulseAudioClient);
 
+        internalUnlock();
+
         if (mpAudioSessionHandler->getId() >= 0) {
-            /* Updates ASM to STOP */
-            if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSessionEvent() == false) {
+            if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSession() == false)
                 mpAudioSessionHandler->updateStop();
-            }
 
-            bool isSkip = mpAudioSessionHandler->isSkipSessionEvent();
-            if (isSkip == false) {
+            if (mpAudioSessionHandler->isSkipSession() == false)
                 mpAudioSessionHandler->unregisterSound();
-            }
         }
 
-        internalUnlock();
-
         CAudioIO::onStateChanged(CAudioInfo::EAudioIOState::AUDIO_IO_STATE_IDLE);
     } catch (CAudioError e) {
         internalUnlock();
@@ -227,7 +230,17 @@ void CAudioOutput::unprepare() throw(CAudioError) {
 
 void CAudioOutput::pause() throw(CAudioError) {
     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) {
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_STATE,
+                        "Can't pause if not in Running state");
+    }
+
+    if (mpPulseAudioClient->isInThread() == true) {
+        THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_OPERATION, "Can't pause in thread");
     }
 
     try {
@@ -236,9 +249,8 @@ void CAudioOutput::pause() throw(CAudioError) {
         internalLock();
 
         /* Updates ASM to STOP */
-        if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSessionEvent() == false) {
+        if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSession() == false)
             mpAudioSessionHandler->updateStop();
-        }
 
         internalUnlock();
 
@@ -251,16 +263,24 @@ void CAudioOutput::pause() throw(CAudioError) {
 
 void CAudioOutput::resume() throw(CAudioError) {
     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) {
+        THROW_ERROR_MSG(CAudioError::EError::ERROR_INVALID_STATE,
+                        "Can't resume if not in Paused state");
+    }
+
+    if (mpPulseAudioClient->isInThread() == true) {
+        THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_OPERATION, "Can't resume in thread");
     }
 
     try {
         internalLock();
 
-        if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSessionEvent() == false) {
-            /* Updates ASM to PLAYING */
+        if (isForceIgnore() == false && mpAudioSessionHandler->isSkipSession() == false)
             mpAudioSessionHandler->updatePlaying();
-        }
 
         internalUnlock();
 
@@ -275,7 +295,8 @@ void CAudioOutput::resume() throw(CAudioError) {
 
 void CAudioOutput::drain() throw(CAudioError) {
     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 {
@@ -287,7 +308,8 @@ void CAudioOutput::drain() throw(CAudioError) {
 
 void CAudioOutput::flush() throw(CAudioError) {
     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 {
@@ -299,7 +321,8 @@ void CAudioOutput::flush() throw(CAudioError) {
 
 int CAudioOutput::getBufferSize() throw(CAudioError) {
     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 */
@@ -308,19 +331,31 @@ int CAudioOutput::getBufferSize() throw(CAudioError) {
 
 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");
+        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,
+                        "Can't write if not in Running state");
     }
 
     /* 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_
+        AUDIO_IO_LOGD("CPulseAudioClient->write(buffer:%p, length:%d)", buffer, length);
+#endif
+
         return length;
     }
 
@@ -330,7 +365,8 @@ size_t CAudioOutput::write(const void* buffer, size_t length) throw(CAudioError)
 
         // 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;
@@ -357,18 +393,18 @@ 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();
+        sched_yield();
     } catch (CAudioError e) {
-        // Unsets synchronous flag
         __mIsUsedSyncWrite = false;
         internalUnlock();
         throw e;