Apply sound manager stream for session managing 11/65811/4
authorky85.kim <ky85.kim@samsung.com>
Tue, 12 Apr 2016 15:07:39 +0000 (00:07 +0900)
committerky85.kim <ky85.kim@samsung.com>
Fri, 15 Apr 2016 00:49:57 +0000 (09:49 +0900)
Change-Id: I41ab9dce7c499d418b0e188ca8ec635f6a457a1f
Signed-off-by: ky85.kim <ky85.kim@samsung.com>
org.tizen.voice.ttsnotiserver.service
org.tizen.voice.ttssrserver.service
server/ttsd_player.c

index 65f9944..ef88ac4 100644 (file)
@@ -1,4 +1,3 @@
 [D-BUS Service]
 Name=org.tizen.voice.ttsnotiserver
 Exec=/usr/bin/tts-daemon-noti
-
index 427b7e5..020af4f 100644 (file)
@@ -1,4 +1,3 @@
 [D-BUS Service]
 Name=org.tizen.voice.ttssrserver
 Exec=/usr/bin/tts-daemon-sr
-
index 256eca2..54699be 100644 (file)
@@ -63,6 +63,8 @@ static int g_sampling_rate;
 
 static audio_out_h g_audio_h;
 
+static sound_stream_info_h g_stream_info_h;
+
 /*
 * Internal Interfaces
 */
@@ -92,32 +94,51 @@ player_s* __player_get_item(int uid)
        return NULL;
 }
 
-void __player_audio_io_interrupted_cb(audio_io_interrupted_code_e code, void *user_data)
+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)
 {
-       SLOG(LOG_DEBUG, get_tag(), "===== INTERRUPTED CALLBACK");
-
-       SLOG(LOG_WARN, get_tag(), "[Player] code : %d", (int)code);
+       SLOG(LOG_DEBUG, get_tag(), "===== Focus state changed cb");
 
-       g_audio_state = AUDIO_STATE_READY;
+       if (stream_info != g_stream_info_h) {
+               SLOG(LOG_ERROR, get_tag(), "[Player ERROR] Invalid stream info handle");
+               return;
+       }
 
-       if (NULL == g_playing_info) {
-               SLOG(LOG_WARN, get_tag(), "[Player WARNING] No current player");
+       int ret;
+       sound_stream_focus_state_e state_for_playback ;
+       ret = sound_manager_get_focus_state(g_stream_info_h, &state_for_playback, NULL);
+       if (SOUND_MANAGER_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, get_tag(), "[Player ERROR] Fail to get focus state");
                return;
        }
 
-       if (APP_STATE_PLAYING == g_playing_info->state) {
-               g_playing_info->state = APP_STATE_PAUSED;
+       SLOG(LOG_WARN, get_tag(), "[Player] focus state changed to (%d) with reason(%d)", (int)state_for_playback, (int)reason_for_change);
 
-               ttsd_data_set_client_state(g_playing_info->uid, APP_STATE_PAUSED);
+       if (AUDIO_STATE_PLAY == g_audio_state && SOUND_STREAM_FOCUS_STATE_RELEASED == state_for_playback) {
+               if (TTSD_MODE_DEFAULT == ttsd_get_mode()) {
+                       g_audio_state = AUDIO_STATE_READY;
+       
+                       if (NULL == g_playing_info) {
+                               SLOG(LOG_WARN, get_tag(), "[Player WARNING] No current player");
+                               return;
+                       }
+
+                       if (APP_STATE_PLAYING == g_playing_info->state) {
+                               g_playing_info->state = APP_STATE_PAUSED;
+       
+                               ttsd_data_set_client_state(g_playing_info->uid, APP_STATE_PAUSED);
 
-               int pid = ttsd_data_get_pid(g_playing_info->uid);
+                               int pid = ttsd_data_get_pid(g_playing_info->uid);
 
-               /* send message to client about changing state */
-               ttsdc_send_set_state_message(pid, g_playing_info->uid, APP_STATE_PAUSED);
+                               /* send message to client about changing state */
+                               ttsdc_send_set_state_message(pid, g_playing_info->uid, APP_STATE_PAUSED);
+                       }
+               } else {
+                       SLOG(LOG_DEBUG, get_tag(), "[Player] Ignore focus state cb - mode(%d)", ttsd_get_mode());
+               }
        }
 
        SLOG(LOG_DEBUG, get_tag(), "=====");
-       SLOG(LOG_DEBUG, get_tag(), "  ");
+       SLOG(LOG_DEBUG, get_tag(), "");
 
        return;
 }
@@ -133,7 +154,7 @@ static int __create_audio_out(ttsp_audio_type_e type, int rate)
                sample_type = AUDIO_SAMPLE_TYPE_U8;
        }
 
-       ret = audio_out_create(rate, AUDIO_CHANNEL_MONO, sample_type, SOUND_TYPE_VOICE, &g_audio_h);
+       ret = audio_out_create_new(rate, AUDIO_CHANNEL_MONO, sample_type, &g_audio_h);
        if (AUDIO_IO_ERROR_NONE != ret) {
                g_audio_state = AUDIO_STATE_NONE;
                g_audio_h = NULL;
@@ -143,24 +164,6 @@ static int __create_audio_out(ttsp_audio_type_e type, int rate)
                SLOG(LOG_DEBUG, get_tag(), "[Player SUCCESS] Create audio");
        }
 
-       if (TTSD_MODE_DEFAULT != ttsd_get_mode()) {
-               ret = audio_out_ignore_session(g_audio_h);
-               if (AUDIO_IO_ERROR_NONE != ret) {
-                       g_audio_state = AUDIO_STATE_NONE;
-                       g_audio_h = NULL;
-                       SLOG(LOG_ERROR, get_tag(), "[Player ERROR] Fail to set ignore session");
-                       return -1;
-               }
-       }
-
-       ret = audio_out_set_interrupted_cb(g_audio_h, __player_audio_io_interrupted_cb, NULL);
-       if (AUDIO_IO_ERROR_NONE != ret) {
-               g_audio_state = AUDIO_STATE_NONE;
-               g_audio_h = NULL;
-               SLOG(LOG_ERROR, get_tag(), "[Player ERROR] Fail to set callback function");
-               return -1;
-       }
-
        g_audio_type = type;
        g_sampling_rate = rate;
 
@@ -199,11 +202,25 @@ static void __end_play_thread(void *data, Ecore_Thread *thread)
        SLOG(LOG_ERROR, get_tag(), "===== End thread");
 }
 
-static void __set_volume_using_voice_policy(int volume)
+static void __set_policy_for_playing(int volume)
 {
+       /* Set stream info */
+       int ret;
+       if (TTSD_MODE_DEFAULT == ttsd_get_mode()) {
+               ret = sound_manager_acquire_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, NULL);
+               if (SOUND_MANAGER_ERROR_NONE != ret) {
+                       SLOG(LOG_WARN, get_tag(), "[Player WARNING] Fail to acquire focus");
+               }
+               ret = audio_out_set_stream_info(g_audio_h, g_stream_info_h);
+               if (AUDIO_IO_ERROR_NONE != ret) {
+                       SLOG(LOG_WARN, get_tag(), "[Player WARNING] Fail to set stream info");
+               }
+       }
+
+       /* Set volume policy */
 /*
        SLOG(LOG_WARN, get_tag(), "[Player WARNING] set volume policy");
-       int ret = sound_manager_set_volume_voice_policy(volume);
+       ret = sound_manager_set_volume_voice_policy(volume);
        if (SOUND_MANAGER_ERROR_NONE != ret) {
                SLOG(LOG_WARN, get_tag(), "[Player WARNING] Fail to set volume policy");
        }
@@ -211,15 +228,25 @@ static void __set_volume_using_voice_policy(int volume)
        return;
 }
 
-static void __unset_volume_using_voice_policy()
+static void __unset_policy_for_playing()
 {
+       int ret;
+       /* Unset volume policy */
 /*
        SLOG(LOG_WARN, get_tag(), "[Player WARNING] unset volume policy");
-       int ret = sound_manager_unset_volume_voice_policy();
+       ret = sound_manager_unset_volume_voice_policy();
        if (SOUND_MANAGER_ERROR_NONE != ret) {
                SLOG(LOG_WARN, get_tag(), "[Player WARNING] Fail to unset volume policy");
        }
 */
+       /* Unset stream info */
+       if (TTSD_MODE_DEFAULT == ttsd_get_mode()) {
+               ret = sound_manager_release_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, NULL);
+               if (SOUND_MANAGER_ERROR_NONE != ret) {
+                       SLOG(LOG_WARN, get_tag(), "[Player WARNING] Fail to release focus");
+               }
+       }
+
        return;
 }
 
@@ -240,7 +267,7 @@ static void __play_thread(void *data, Ecore_Thread *thread)
        int idx = 0;
 
        /* set volume policy as 40% */
-       __set_volume_using_voice_policy(40);
+       __set_policy_for_playing(40);
        while (1) {
                if (true == player->is_paused_data) {
                        /* Resume player */
@@ -264,7 +291,7 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                                g_audio_state = AUDIO_STATE_READY;
 
                                /* unset volume policy, volume will be 100% */
-                               __unset_volume_using_voice_policy();
+                               __unset_policy_for_playing();
                                return;
                        }
                        SLOG(LOG_INFO, get_tag(), "[Player] Sound info : id(%d) data(%p) size(%d) audiotype(%d) rate(%d) event(%d)", 
@@ -285,7 +312,7 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                                g_audio_state = AUDIO_STATE_READY;
 
                                /* unset volume policy, volume will be 100% */
-                               __unset_volume_using_voice_policy();
+                               __unset_policy_for_playing();
 
                                /* wait for new audio data come */
                                while (1) {
@@ -302,7 +329,7 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                                }
 
                                /* set volume policy as 40%, when resume play thread*/
-                               __set_volume_using_voice_policy(40);
+                               __set_policy_for_playing(40);
 
                                /* resume play thread */
                                player->state = APP_STATE_PLAYING;
@@ -321,7 +348,7 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                                if (pid <= 0) {
                                        SLOG(LOG_WARN, get_tag(), "[Send WARNIING] Current player is not valid");
                                        /* unset volume policy, volume will be 100% */
-                                       __unset_volume_using_voice_policy();
+                                       __unset_policy_for_playing();
                                        return;
                                }
 
@@ -345,7 +372,7 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                                        if (pid <= 0) {
                                                SLOG(LOG_WARN, get_tag(), "[Send WARNIING] Current player is not valid");
                                                /* unset volume policy, volume will be 100% */
-                                               __unset_volume_using_voice_policy();
+                                               __unset_policy_for_playing();
                                                return;
                                        }
                                        if (0 != ttsdc_send_utt_finish_message(pid, player->uid, sound_data->utt_id)) {
@@ -367,7 +394,7 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                        if (0 > __create_audio_out(sound_data->audio_type, sound_data->rate)) {
                                SLOG(LOG_ERROR, get_tag(), "[Player ERROR] Fail to create audio out");
                                /* unset volume policy, volume will be 100% */
-                               __unset_volume_using_voice_policy();
+                               __unset_policy_for_playing();
                                return;
                        }
                }
@@ -389,7 +416,7 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                                        SLOG(LOG_ERROR, get_tag(), "[Player ERROR] Fail to prepare audio : %d", ret);
                                        g_playing_info = NULL;
                                        /* unset volume policy, volume will be 100% */
-                                       __unset_volume_using_voice_policy();
+                                       __unset_policy_for_playing();
                                        return;
                                }
                                SLOG(LOG_DEBUG, get_tag(), "[Player SUCCESS] Prepare audio");
@@ -412,7 +439,7 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                                        SLOG(LOG_ERROR, get_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
                                }
                                /* unset volume policy, volume will be 100% */
-                               __unset_volume_using_voice_policy();
+                               __unset_policy_for_playing();
 
                                if (NULL != sound_data) {
                                        if (NULL != sound_data->data) {
@@ -445,7 +472,7 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                                        SLOG(LOG_DEBUG, get_tag(), "[Player SUCCESS] Unprepare audio");
                                }
                                /* unset volume policy, volume will be 100% */
-                               __unset_volume_using_voice_policy();
+                               __unset_policy_for_playing();
                                return;
                        }
                }
@@ -473,7 +500,7 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                                sound_data = NULL;
                        }
                        /* unset volume policy, volume will be 100% */
-                       __unset_volume_using_voice_policy();
+                       __unset_policy_for_playing();
                        return;
                }
 
@@ -485,7 +512,7 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                        if (pid <= 0) {
                                SLOG(LOG_WARN, get_tag(), "[Send WARNIING] Current player is not valid");
                                /* unset volume policy, volume will be 100% */
-                               __unset_volume_using_voice_policy();
+                               __unset_policy_for_playing();
                                return;
                        }
 
@@ -493,7 +520,7 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                                SLOG(LOG_ERROR, get_tag(), "[Send ERROR] Fail to send Utterance Completed Signal : pid(%d), uid(%d), uttid(%d)", 
                                        pid, player->uid, sound_data->utt_id);
                                /* unset volume policy, volume will be 100% */
-                               __unset_volume_using_voice_policy();
+                               __unset_policy_for_playing();
                                return;
                        }
 
@@ -518,7 +545,7 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                                SLOG(LOG_ERROR, get_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
                        }
                        /* unset volume policy, volume will be 100% */
-                       __unset_volume_using_voice_policy();
+                       __unset_policy_for_playing();
 
                        return;
                }
@@ -536,22 +563,16 @@ int ttsd_player_init()
 
        int ret;
 
-       if (TTSD_MODE_DEFAULT == ttsd_get_mode()) {
-               ret = sound_manager_set_session_type(SOUND_SESSION_TYPE_MEDIA);
-               if (0 != ret) {
-                       SLOG(LOG_ERROR, get_tag(), "[Player ERROR] Fail to set session type");
-               }
+       ecore_thread_max_set(1);
 
-               ret = sound_manager_set_media_session_option(SOUND_SESSION_OPTION_PAUSE_OTHERS_WHEN_START, SOUND_SESSION_OPTION_INTERRUPTIBLE_DURING_PLAY);
-               if (0 != ret) {
-                       SLOG(LOG_ERROR, get_tag(), "[Player ERROR] Fail set media session option");
-               } else {
-                       SLOG(LOG_DEBUG, get_tag(), "[Player SUCCESS] set media session option");
-               }
+       ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_VOICE_INFORMATION, __player_focus_state_cb, NULL, &g_stream_info_h);
+       if (SOUND_MANAGER_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, get_tag(), "[Player ERROR] Fail to create stream info");
+               return -1;
+       } else {
+               SLOG(LOG_DEBUG, get_tag(), "[Player SUCCESS] Create stream info");
        }
 
-       ecore_thread_max_set(1);
-
        ret = __create_audio_out(TTSP_AUDIO_TYPE_RAW_S16, 16000);
        if (0 != ret)
                return -1;
@@ -595,6 +616,13 @@ int ttsd_player_release(void)
        if (0 != ret)
                return -1;
 
+       ret = sound_manager_destroy_stream_information(g_stream_info_h);
+       if (SOUND_MANAGER_ERROR_NONE != ret) {
+               SLOG(LOG_WARN, get_tag(), "[Player WARNING] Fail to destroy stream info");
+       } else {
+               SLOG(LOG_DEBUG, get_tag(), "[Player SUCCESS] Destroy stream info");
+       }
+
        /* clear g_player_list */
        g_playing_info = NULL;
        g_player_init = false;