Stop playing sound when starting STT
[platform/core/uifw/inputdelegator.git] / src / SttManager.cpp
index 1be05a3..c4a080e 100755 (executable)
@@ -17,6 +17,7 @@
 #include <assert.h>
 #include <vconf.h>
 #include <vconf-keys.h>
+#include <sound_manager.h>
 
 #include "Debug.h"
 #include "SttManager.h"
@@ -30,6 +31,8 @@ enum {
        CREATE = 0x1000
 };
 
+static sound_stream_info_h g_stream_info_h = NULL;
+
 static inline const char *stt_state_str(stt_state_e cur) {
        if (cur == STT_STATE_CREATED)
                return (const char *) "STT_STATE_CREATED";
@@ -47,6 +50,10 @@ static inline const char *stt_state_str(stt_state_e cur) {
                return (const char *) "ABNORMAL CASE";
 }
 
+static void player_focus_state_cb(sound_stream_info_h stream_info, sound_stream_focus_change_reason_e reason_for_change, const char *extra_info, void *user_data)
+{
+}
+
 SttManager::SttManager(ISttFeedback& feedback)
 : ifeedback(feedback),
   iscancelled(false)
@@ -61,6 +68,11 @@ SttManager::SttManager(ISttFeedback& feedback)
                if(ret != STT_ERROR_NONE)
                        throw SttException(ret, ErrorString((stt_error_e)ret));
 
+               ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_VOICE_RECOGNITION, player_focus_state_cb, NULL, &g_stream_info_h);
+               if (SOUND_MANAGER_ERROR_NONE != ret) {
+                       LOGW("Fail to create stream info. ret : %d", ret);
+               }
+
                /**
                * Set default properties
                *
@@ -84,6 +96,10 @@ SttManager::~SttManager() {
                PRINTFUNC(DLOG_ERROR, "reason : %s", e.what());
                stt_destroy(handle);
        }
+
+       ReleaseSoundFocus();
+
+       sound_manager_destroy_stream_information(g_stream_info_h);
 }
 
 void SttManager::Prepare() {
@@ -123,7 +139,13 @@ void SttManager::Start() {
        */
        asrtype = STT_RECOGNITION_TYPE_FREE_PARTIAL;
        int ret;
-        ret = stt_start(handle, language.c_str(), asrtype.c_str());
+
+       ret = sound_manager_acquire_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, NULL);
+       if (SOUND_MANAGER_ERROR_NONE != ret) {
+               LOGW("Fail to acquire focus. ret : %d, stream handle : %p", ret, g_stream_info_h);
+       }
+
+       ret = stt_start(handle, language.c_str(), asrtype.c_str());
 
        if(ret != STT_ERROR_NONE)
                throw SttException(ret, ErrorString((stt_error_e)ret));
@@ -318,6 +340,11 @@ void SttManager::on_state_changed(
        SttManager& manager = *((SttManager *) user_data);
 
        if (current== STT_STATE_READY) {
+               if (previous == STT_STATE_RECORDING ||
+                       previous == STT_STATE_PROCESSING) {
+                       ReleaseSoundFocus();
+               }
+
                if (previous == STT_STATE_CREATED) {
                        manager.EnableSilenceDetection();
                        manager.ifeedback.AutoStart();
@@ -525,3 +552,10 @@ void SttManager::EnableSilenceDetection(bool enabled) {
        }
 }
 
+void SttManager::ReleaseSoundFocus()
+{
+       int ret = sound_manager_release_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, NULL);
+       if (SOUND_MANAGER_ERROR_NONE != ret) {
+               LOGW("Fail to release focus. ret : %d", ret);
+       }
+}