Merge "Fix crash when player_focus_state_cb is called" into tizen
[platform/core/uifw/tts.git] / server / ttsd_player.c
index 6c21cbb..ec6a4db 100644 (file)
@@ -44,6 +44,7 @@ typedef struct {
 } player_s;
 
 #define SOUND_BUFFER_LENGTH    2048
+#define FOCUS_SERVER_READY "/tmp/.sound_server_ready"
 
 /** player init info */
 static bool g_player_init = false;
@@ -94,51 +95,45 @@ player_s* __player_get_item(int uid)
        return NULL;
 }
 
-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)
+void __player_focus_state_cb(sound_stream_info_h stream_info, sound_stream_focus_mask_e focus_mask, sound_stream_focus_state_e focus_state,
+                                                       sound_stream_focus_change_reason_e reason_for_change, int sound_behavior, const char *extra_info, void *user_data)
 {
-       SLOG(LOG_DEBUG, tts_tag(), "===== Focus state changed cb");
+       SLOG(LOG_DEBUG, tts_tag(), "@@@ Focus state changed cb");
 
        if (stream_info != g_stream_info_h) {
                SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Invalid stream info handle");
                return;
        }
+       SLOG(LOG_WARN, tts_tag(), "[Player] focus state changed to (%d) with reason(%d)", (int)focus_state, (int)reason_for_change);
 
-       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, tts_tag(), "[Player ERROR] Fail to get focus state");
-               return;
-       }
-
-       SLOG(LOG_WARN, tts_tag(), "[Player] focus state changed to (%d) with reason(%d)", (int)state_for_playback, (int)reason_for_change);
-
-       if (AUDIO_STATE_PLAY == g_audio_state && SOUND_STREAM_FOCUS_STATE_RELEASED == state_for_playback) {
+       if (AUDIO_STATE_PLAY == g_audio_state && focus_mask == SOUND_STREAM_FOCUS_FOR_PLAYBACK && SOUND_STREAM_FOCUS_STATE_RELEASED == focus_state) {
                if (TTSD_MODE_DEFAULT == ttsd_get_mode()) {
                        g_audio_state = AUDIO_STATE_READY;
-       
+
                        if (NULL == g_playing_info) {
-               SLOG(LOG_WARN, tts_tag(), "[Player WARNING] No current player");
+                               SLOG(LOG_WARN, tts_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 uid = g_playing_info->uid;
 
-                               int pid = ttsd_data_get_pid(g_playing_info->uid);
+                               if (0 != ttsd_player_pause(uid)) {
+                                       SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to pause the player");
+                                       return;
+                               }
 
+                               ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
+                               int pid = ttsd_data_get_pid(uid);
                                /* send message to client about changing state */
-                               ttsdc_send_set_state_message(pid, g_playing_info->uid, APP_STATE_PAUSED);
+                               ttsdc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
                        }
                } else {
                        SLOG(LOG_DEBUG, tts_tag(), "[Player] Ignore focus state cb - mode(%d)", ttsd_get_mode());
                }
        }
 
-       SLOG(LOG_DEBUG, tts_tag(), "=====");
-       SLOG(LOG_DEBUG, tts_tag(), "");
+       SLOG(LOG_DEBUG, tts_tag(), "@@@");
 
        return;
 }
@@ -199,7 +194,7 @@ static int __destroy_audio_out()
 
 static void __end_play_thread(void *data, Ecore_Thread *thread)
 {
-       SLOG(LOG_ERROR, tts_tag(), "===== End thread");
+       SLOG(LOG_ERROR, tts_tag(), "@@@ End thread");
 }
 
 static void __set_policy_for_playing(int volume)
@@ -207,12 +202,12 @@ 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);
+               ret = sound_manager_acquire_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, SOUND_BEHAVIOR_NONE, NULL);
                if (SOUND_MANAGER_ERROR_NONE != ret) {
                        SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to acquire focus");
                }
        }
-       ret = audio_out_set_stream_info(g_audio_h, g_stream_info_h);
+       ret = audio_out_set_sound_stream_info(g_audio_h, g_stream_info_h);
        if (AUDIO_IO_ERROR_NONE != ret) {
                SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to set stream info");
        }
@@ -225,9 +220,17 @@ static void __unset_policy_for_playing()
        int ret;
        /* 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);
+               sound_stream_focus_state_e state_for_playing = SOUND_STREAM_FOCUS_STATE_ACQUIRED;
+               ret = sound_manager_get_focus_state(g_stream_info_h, &state_for_playing, NULL);
                if (SOUND_MANAGER_ERROR_NONE != ret) {
-                       SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to release focus");
+                       SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to get focus state: %d", ret);
+               }
+
+               if (SOUND_STREAM_FOCUS_STATE_ACQUIRED == state_for_playing) {
+                       ret = sound_manager_release_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, SOUND_BEHAVIOR_NONE, NULL);
+                       if (SOUND_MANAGER_ERROR_NONE != ret) {
+                               SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to release focus");
+                       }
                }
        }
 
@@ -236,7 +239,7 @@ static void __unset_policy_for_playing()
 
 static void __play_thread(void *data, Ecore_Thread *thread)
 {
-       SLOG(LOG_DEBUG, tts_tag(), "===== Start thread");
+       SLOG(LOG_DEBUG, tts_tag(), "@@@ Start thread");
 
        if (NULL == g_playing_info) {
                SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] No current player");
@@ -303,15 +306,17 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                                        usleep(10000);
                                        if (NULL == g_playing_info) {
                                                /* current playing uid is replaced */
-                                               SLOG(LOG_DEBUG, tts_tag(), "[Player] Finish thread");
+                                               SLOG(LOG_INFO, tts_tag(), "[Player] Finish thread");
                                                return;
                                        } else if (0 < ttsd_data_get_sound_data_size(player->uid)) {
                                                /* new audio data come */
-                                               SLOG(LOG_DEBUG, tts_tag(), "[Player] Resume thread");
+                                               SLOG(LOG_INFO, tts_tag(), "[Player] Resume thread");
                                                break;
                                        }
                                }
 
+                               SLOG(LOG_INFO, tts_tag(), "[Player] Finish to wait for new audio data come");
+
                                /* set volume policy as 40%, when resume play thread*/
                                __set_policy_for_playing(40);
 
@@ -333,6 +338,13 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                                        SLOG(LOG_WARN, tts_tag(), "[Send WARNIING] Current player is not valid");
                                        /* unset volume policy, volume will be 100% */
                                        __unset_policy_for_playing();
+
+                                       if (NULL != sound_data->data) {
+                                               free(sound_data->data);
+                                               sound_data->data = NULL;
+                                       }
+                                       free(sound_data);
+                                       sound_data = NULL;
                                        return;
                                }
 
@@ -340,7 +352,7 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                                        SLOG(LOG_ERROR, tts_tag(), "[Send ERROR] Fail to send Utterance Start Signal : pid(%d), uid(%d), uttid(%d)", 
                                                pid, player->uid, sound_data->utt_id);
                                }
-                               SLOG(LOG_DEBUG, tts_tag(), "[Player] Start utterance : uid(%d), uttid(%d)", player->uid, sound_data->utt_id);
+                               SLOG(LOG_INFO, tts_tag(), "[Player] Start utterance : uid(%d), uttid(%d)", player->uid, sound_data->utt_id);
                        }
 
                        /* Save last event to check utterance start */
@@ -357,6 +369,12 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                                                SLOG(LOG_WARN, tts_tag(), "[Send WARNIING] Current player is not valid");
                                                /* unset volume policy, volume will be 100% */
                                                __unset_policy_for_playing();
+                                               if (NULL != sound_data->data) {
+                                                       free(sound_data->data);
+                                                       sound_data->data = NULL;
+                                               }
+                                               free(sound_data);
+                                               sound_data = NULL;
                                                return;
                                        }
                                        if (0 != ttsdc_send_utt_finish_message(pid, player->uid, sound_data->utt_id)) {
@@ -364,7 +382,13 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                                                        pid, player->uid, sound_data->utt_id);
                                        }
                                }
-                               SLOG(LOG_DEBUG, tts_tag(), "[Player] Finish utterance : uid(%d), uttid(%d)", player->uid, sound_data->utt_id);
+                               SLOG(LOG_INFO, tts_tag(), "[Player] Finish utterance : uid(%d), uttid(%d)", player->uid, sound_data->utt_id);
+                               if (NULL != sound_data->data) {
+                                       free(sound_data->data);
+                                       sound_data->data = NULL;
+                               }
+                               free(sound_data);
+                               sound_data = NULL;
                                continue;
                        }
                }
@@ -379,8 +403,17 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                                SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to create audio out");
                                /* unset volume policy, volume will be 100% */
                                __unset_policy_for_playing();
+
+                               if (NULL != sound_data->data) {
+                                       free(sound_data->data);
+                                       sound_data->data = NULL;
+                               }
+                               free(sound_data);
+                               sound_data = NULL;
+
                                return;
                        }
+                       __set_policy_for_playing(40);
                }
 
                while (APP_STATE_PLAYING == player->state || APP_STATE_PAUSED == player->state) {
@@ -401,6 +434,15 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                                        g_playing_info = NULL;
                                        /* unset volume policy, volume will be 100% */
                                        __unset_policy_for_playing();
+
+                                       if (NULL != sound_data->data) {
+                                               free(sound_data->data);
+                                               sound_data->data = NULL;
+                                       }
+
+                                       free(sound_data);
+                                       sound_data = NULL;
+
                                        return;
                                }
                                SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Prepare audio");
@@ -446,7 +488,7 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                                player->idx = idx;
 
                                g_audio_state = AUDIO_STATE_READY;
-                               SLOG(LOG_DEBUG, tts_tag(), "[Player] Stop player thread by pause");
+                               SLOG(LOG_INFO, tts_tag(), "[Player] Stop player thread by pause");
 
                                /* Request prepare */
                                ret = audio_out_unprepare(g_audio_h);
@@ -497,6 +539,13 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                                SLOG(LOG_WARN, tts_tag(), "[Send WARNIING] Current player is not valid");
                                /* unset volume policy, volume will be 100% */
                                __unset_policy_for_playing();
+                               if (NULL != sound_data->data) {
+                                       free(sound_data->data);
+                                       sound_data->data = NULL;
+                               }
+
+                               free(sound_data);
+                               sound_data = NULL;
                                return;
                        }
 
@@ -505,10 +554,17 @@ static void __play_thread(void *data, Ecore_Thread *thread)
                                        pid, player->uid, sound_data->utt_id);
                                /* unset volume policy, volume will be 100% */
                                __unset_policy_for_playing();
+                               if (NULL != sound_data->data) {
+                                       free(sound_data->data);
+                                       sound_data->data = NULL;
+                               }
+
+                               free(sound_data);
+                               sound_data = NULL;
                                return;
                        }
 
-                       SLOG(LOG_DEBUG, tts_tag(), "[Player] Finish utterance : uid(%d), uttid(%d)", player->uid, sound_data->utt_id);
+                       SLOG(LOG_INFO, tts_tag(), "[Player] Finish utterance : uid(%d), uttid(%d)", player->uid, sound_data->utt_id);
                }
 
                if (NULL != sound_data) {
@@ -549,6 +605,18 @@ int ttsd_player_init()
 
        ecore_thread_max_set(1);
 
+       int cnt = 0;
+       while (1) {
+               if (0 == access(FOCUS_SERVER_READY, F_OK)) {
+                       SLOG(LOG_ERROR, tts_tag(), "[Player SUCCESS] focus server is available");
+                       break;
+               } else {
+                       if (0 == cnt++ % 10)
+                               SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] focus server is not available");
+                       usleep(50000);
+               }
+       }
+
        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, tts_tag(), "[Player ERROR] Fail to create stream info");
@@ -577,9 +645,9 @@ int ttsd_player_release(void)
 
        int ret;
 
-       SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] ==========================");
+       SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] @@@@@");
        SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Active thread count : %d", ecore_thread_active_get());
-       SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] ==========================");
+       SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] @@@@@");
 
        /* The thread should be released */
        int thread_count = ecore_thread_active_get();
@@ -737,10 +805,10 @@ int ttsd_player_play(int uid)
 
        g_playing_info = current;
 
-       SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Active thread count : %d", ecore_thread_active_get());
+       SLOG(LOG_INFO, tts_tag(), "[Player DEBUG] Active thread count : %d", ecore_thread_active_get());
 
        if (0 < ttsd_data_get_sound_data_size(current->uid)) {
-               SLOG(LOG_DEBUG, tts_tag(), "[Player] Run thread");
+               SLOG(LOG_INFO, tts_tag(), "[Player] Run thread");
                ecore_thread_run(__play_thread, __end_play_thread, NULL, NULL);
        }
 
@@ -790,9 +858,9 @@ int ttsd_player_stop(int uid)
        current->idx = 0;
 
        if (NULL == g_playing_info) {
-               SLOG(LOG_DEBUG, tts_tag(), "[Player] ==========================");
+               SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
                SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
-               SLOG(LOG_DEBUG, tts_tag(), "[Player] ==========================");
+               SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
 
                /* The thread should be released */
                int thread_count = ecore_thread_active_get();
@@ -809,12 +877,12 @@ int ttsd_player_stop(int uid)
                        thread_count = ecore_thread_active_get();
                }
 
-               SLOG(LOG_DEBUG, tts_tag(), "[Player] ==========================");
+               SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
                SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
-               SLOG(LOG_DEBUG, tts_tag(), "[Player] ==========================");
+               SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
        }
 
-       SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Stop player : uid(%d)", uid);
+       SLOG(LOG_INFO, tts_tag(), "[Player SUCCESS] Stop player : uid(%d)", uid);
 
        return 0;
 }
@@ -884,10 +952,10 @@ int ttsd_player_pause(int uid)
        }
 
        current->state = APP_STATE_PAUSED;
-       
-       SLOG(LOG_DEBUG, tts_tag(), "[Player] ==========================");
+
+       SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
        SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
-       SLOG(LOG_DEBUG, tts_tag(), "[Player] ==========================");
+       SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
 
        /* The thread should be released */
        int thread_count = ecore_thread_active_get();
@@ -904,9 +972,9 @@ int ttsd_player_pause(int uid)
                thread_count = ecore_thread_active_get();
        }
 
-       SLOG(LOG_DEBUG, tts_tag(), "[Player] ==========================");
+       SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
        SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
-       SLOG(LOG_DEBUG, tts_tag(), "[Player] ==========================");
+       SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
 
        SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Pause player : uid(%d)", uid);
 
@@ -937,7 +1005,7 @@ int ttsd_player_resume(int uid)
        current->state = APP_STATE_PLAYING;
        g_playing_info = current;
 
-       SLOG(LOG_DEBUG, tts_tag(), "[Player] Run thread");
+       SLOG(LOG_INFO, tts_tag(), "[Player] Resume to run thread");
        ecore_thread_run(__play_thread, __end_play_thread, NULL, NULL);
 
        return 0;