Rearrange the policy handling code 91/271891/2
authorSuyeon Hwang <stom.hwang@samsung.com>
Wed, 19 Jan 2022 05:08:37 +0000 (14:08 +0900)
committerSuyeon Hwang <stom.hwang@samsung.com>
Tue, 15 Mar 2022 05:47:40 +0000 (14:47 +0900)
In order to service by one process, server would handle policy for client mode without sound focus.

This patch removes the sound focus changed callback from ttsd_player because it is not used from
now. And this patch changes the policy code in ttsd_server to make the code cover all cases of
policy.

Change-Id: I48f92e4453d7299df805af938bbb1fdac2403e3b
Signed-off-by: Suyeon Hwang <stom.hwang@samsung.com>
server/ttsd_player.cpp
server/ttsd_player.h
server/ttsd_server.c

index 8d5e09f8fe6daeddf0917567e89c6da0d81eed00..af13e0c91fec24cbb4c8f95a92f30925a8016dbf 100644 (file)
@@ -128,57 +128,6 @@ static void __write_buffer_dump_file(const void* buffer, size_t length)
 }
 #endif
 
-static void __focus_release_callback()
-{
-       unsigned int uid = g_player_thread->getCurrentUid();
-       if (APP_STATE_PLAYING != ttsd_data_get_client_state(uid)) {
-               return;
-       }
-
-       ttsd_mode_e mode = ttsd_data_get_mode(uid);
-       switch (mode) {
-       case TTSD_MODE_DEFAULT:
-               {
-                       SLOG(LOG_DEBUG, tts_tag(), "[Player] Pause current player - mode(%d)", mode);
-                       if (0 != ttsd_player_pause(uid)) {
-                               SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to pause the player");
-                               break;
-                       }
-
-                       ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
-                       int pid = ttsd_data_get_pid(uid);
-                       if (pid <= 0) {
-                               SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to get pid. uid(%u)", uid);
-                       } else {
-                               /* send message to client about changing state */
-                               SLOG(LOG_INFO, tts_tag(), "[Player INFO] Player paused. pid(%d), uid(%u)", pid, uid);
-                               ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
-                       }
-                       break;
-               }
-
-       case TTSD_MODE_NOTIFICATION:
-       case TTSD_MODE_SCREEN_READER:
-               {
-                       SLOG(LOG_DEBUG, tts_tag(), "[Player] Stop current player - mode(%d)", mode);
-                       ttsd_send_all_stop();
-                       break;
-               }
-
-       case TTSD_MODE_INTERRUPT:
-               SLOG(LOG_DEBUG, tts_tag(), "[Player] Ignore focus release - mode(%d)", mode);
-               break;
-
-       default:
-               SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Invalid mode - mode(%d)", mode);
-               break;
-       }
-
-       SLOG(LOG_DEBUG, tts_tag(), "@@@");
-
-       return;
-}
-
 static void __set_policy_for_playing(void)
 {
        const char* extra_info = nullptr;
@@ -202,35 +151,6 @@ static void __unset_policy_for_playing()
        SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] unset policy for playing");
 }
 
-static bool __does_interrupt_have_focus(sound_stream_focus_change_reason_e reason, int sound_behavior, char *extra_info)
-{
-       SLOG(LOG_DEBUG, tts_tag(), "[Player] current Playback focus: extra_info(%s), reason(%d), sound_behavior(%d)", extra_info, reason, sound_behavior);
-       if (SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_INFORMATION != reason) {
-               return false;
-       }
-
-       if (nullptr == extra_info || 0 >= strlen(extra_info) || 0 != strncmp(extra_info, "TTSD_MODE_INTERRUPT", EXTRA_INFO_LENGTH)) {
-               return false;
-       }
-
-       return true;
-}
-
-bool ttsd_player_does_interrupt_have_playback_focus()
-{
-       sound_stream_focus_change_reason_e reason;
-       int sound_behavior = 0;
-       char *extra_info = nullptr;
-       if (SOUND_MANAGER_ERROR_NONE != sound_manager_get_current_playback_focus(&reason, &sound_behavior, &extra_info)) {
-               SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to get focus information");
-               return false;
-       }
-
-       bool result = __does_interrupt_have_focus(reason, sound_behavior, extra_info);
-       free(extra_info);
-       return result;
-}
-
 static int __notify_utterance_started_event(unsigned int uid, int utt_id)
 {
        int pid = ttsd_data_get_pid(uid);
@@ -473,7 +393,7 @@ static void __play_utterance_cb(PlayerThread* player, unsigned int uid)
 int ttsd_player_init()
 {
        g_background_volume = new BackgroundVolume(SND_MGR_DUCKING_DURATION);
-       g_audio_stream = new AudioStream(__focus_release_callback);
+       g_audio_stream = new AudioStream(nullptr);
        g_player_thread = new PlayerThread(__play_utterance_cb);
 
        g_is_set_policy = false;
index ef850a0928dbd05b27e569b114133d6da6518b35..d029ccb2f1ed1e1b4d68a0d3398f66c5ea2e4ce8 100644 (file)
@@ -49,8 +49,6 @@ int ttsd_player_resume(unsigned int uid);
 
 int ttsd_player_all_stop();
 
-bool ttsd_player_does_interrupt_have_playback_focus();
-
 int ttsd_player_set_background_volume_ratio(double ratio);
 
 int ttsd_player_wait_to_play(unsigned int uid);
index 93244314f8847e04f1299047d0b84fecbaea06ce..83218dd45b1863fe30443c4d362225ad97f8cd86 100644 (file)
@@ -57,6 +57,25 @@ static bool g_is_terminated = false;
 
 
 /* Function definitions */
+static int __stop_and_send_ready_state(unsigned int uid)
+{
+       int ret = ttsd_server_stop(uid);
+       if (TTSD_ERROR_NONE != ret) {
+               SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_server_stop() : uid(%u). ret(%d)", uid, ret);
+               return ret;
+       }
+
+       int pid = ttsd_data_get_pid(uid);
+       if (pid <= 0) {
+               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%u)", uid);
+               return TTSD_ERROR_OPERATION_FAILED;
+       } else {
+               ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
+       }
+
+       return ret;
+}
+
 static void __synthesis(unsigned int uid);
 
 static Eina_Bool __wait_synthesis(void *data)
@@ -110,8 +129,7 @@ static void __synthesis(unsigned int uid)
 
        if (NULL == speak_data->lang || NULL == speak_data->text) {
                SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Current data is NOT valid");
-               ttsd_server_stop(uid);
-               ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
+               __stop_and_send_ready_state(uid);
                ttsd_data_destroy_speak_data(speak_data);
                speak_data = NULL;
                return;
@@ -135,14 +153,7 @@ static void __synthesis(unsigned int uid)
                SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] * FAIL to start SYNTHESIS !!!! * ");
 
                ttsd_set_synth_control(TTSD_SYNTHESIS_CONTROL_DONE);
-               ttsd_server_stop(uid);
-
-               int pid = ttsd_data_get_pid(uid);
-               if (pid <= 0) {
-                       SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Fail to send set_state_message. uid(%u)", uid);
-               } else {
-                       ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_READY);
-               }
+               __stop_and_send_ready_state(uid);
        } else {
                g_wait_timer = ecore_timer_add(0.05, __wait_synthesis, (void*)credential);
        }
@@ -927,24 +938,41 @@ int ttsd_server_add_text(unsigned int uid, const char* text, const char* lang, i
        return TTSD_ERROR_NONE;
 }
 
-void __send_interrupt_client(unsigned int uid)
+static int __interrupt_player_by_policy(unsigned int request_app_uid)
 {
-       SLOG(LOG_INFO, tts_tag(), "[Server] Send new state by policy. uid(%u)", uid);
-       int pid = ttsd_data_get_pid(uid);
-       if (pid <= 0) {
-               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get pid. uid(%u), pid(%d)", uid, pid);
-               return;
+       unsigned int playing_app_uid = ttsd_data_get_current_playing();
+       if (0 > ttsd_data_is_client(playing_app_uid)) {
+               SLOG(LOG_INFO, tts_tag(), "[Server] There is no app which is in 'Play' state");
+               return TTSD_ERROR_NONE;
        }
 
-       app_tts_state_e state = ttsd_data_get_client_state(uid);
-       if (APP_STATE_NONE == state) {
-               SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get state. uid(%u), pid(%d)", uid, pid);
-               return;
+       if (request_app_uid == playing_app_uid) {
+               SLOG(LOG_WARN, tts_tag(), "[Server] This is played uid. Skip policy behavior");
+               return TTSD_ERROR_NONE;
        }
 
-       ttsdc_ipc_send_set_state_message(pid, uid, state);
-       SLOG(LOG_INFO, tts_tag(), "[Server] Finish to send new state by policy. uid(%u)", uid);
-       return;
+       ttsd_mode_e request_app_mode = ttsd_data_get_mode(request_app_uid);
+       ttsd_mode_e playing_app_mode = ttsd_data_get_mode(playing_app_uid);
+       switch (playing_app_mode) {
+       case TTSD_MODE_DEFAULT:
+       case TTSD_MODE_NOTIFICATION:
+       case TTSD_MODE_SCREEN_READER:
+               SLOG(LOG_DEBUG, tts_tag(), "[Server] Played client(%u) will be interrupted into 'Stop' state", playing_app_uid);
+               __stop_and_send_ready_state(playing_app_uid);
+               break;
+
+       case TTSD_MODE_INTERRUPT:
+               if (TTSD_MODE_INTERRUPT != request_app_mode) {
+                       SLOG(LOG_WARN, tts_tag(), "[Server] Current play mode is Interrupt. Default, Screen reader, and Noti are prohibitted.");
+                       return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
+               }
+
+               SLOG(LOG_DEBUG, tts_tag(), "[Server] Played client(%u) will be interrupted into 'Stop' state", playing_app_uid);
+               __stop_and_send_ready_state(playing_app_uid);
+               break;
+       }
+
+       return TTSD_ERROR_NONE;
 }
 
 int ttsd_server_play(unsigned int uid, const char* credential)
@@ -968,38 +996,10 @@ int ttsd_server_play(unsigned int uid, const char* credential)
                }
        }
 
-       /* check the current playback focus */
-       if (TTSD_MODE_INTERRUPT != ttsd_get_mode() && ttsd_player_does_interrupt_have_playback_focus()) {
-               SLOG(LOG_WARN, tts_tag(), "[Server WARNING] Current playback focus is set on Interrupt mode. Cannot play default, screen reader, and noti modes.");
-               ttsd_data_clear_data(uid);
-               return TTSD_ERROR_AUDIO_POLICY_BLOCKED;
-       }
-
-       unsigned int current_uid = ttsd_data_get_current_playing();
-       SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%u)", current_uid);
-
-       if (uid != current_uid && 0 <= ttsd_data_is_client(current_uid)) {
-               if (TTSD_MODE_DEFAULT != ttsd_data_get_mode(current_uid)) {
-                       /* Send interrupt message */
-                       SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Stop' state ", current_uid);
-
-                       /* stop player */
-                       if (0 != ttsd_server_stop(current_uid)) {
-                               SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%u)", current_uid);
-                       }
-               } else {
-                       /* Default mode policy of interrupt is "Pause" */
-
-                       /* Send interrupt message */
-                       SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Pause' state ", current_uid);
-
-                       /* pause player */
-                       if (0 != ttsd_server_pause(current_uid)) {
-                               SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_server_pause() : uid (%u)", current_uid);
-                       }
-               }
-
-               __send_interrupt_client(current_uid);
+       int ret = __interrupt_player_by_policy(uid);
+       if (TTSD_ERROR_NONE != ret) {
+               SLOG(LOG_WARN, tts_tag(), "[Server] Current play mode is Interrupt. Cannot play default, screen reader, and noti modes.");
+               return ret;
        }
 
        /* Change current play */
@@ -1012,7 +1012,6 @@ int ttsd_server_play(unsigned int uid, const char* credential)
                return TTSD_ERROR_OPERATION_FAILED;
        }
 
-       int ret = TTSD_ERROR_NONE;
        if (APP_STATE_PAUSED == state) {
                SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%u) is 'Pause' state : resume player", uid);
                ret = ttsd_player_resume(uid);
@@ -1238,31 +1237,10 @@ int ttsd_server_play_pcm(unsigned int uid)
                return TTSD_ERROR_NONE;
        }
 
-       unsigned int current_uid = ttsd_data_get_current_playing();
-       SLOG(LOG_INFO, tts_tag(), "[Server] playing uid (%u)", current_uid);
-
-       if (uid != current_uid && 0 <= ttsd_data_is_client(current_uid)) {
-               if (TTSD_MODE_DEFAULT != ttsd_data_get_mode(current_uid)) {
-                       /* Send interrupt message */
-                       SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Stop' state ", current_uid);
-
-                       /* stop player */
-                       if (0 != ttsd_server_stop(current_uid)) {
-                               SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to stop server and player : uid (%u)", current_uid);
-                       }
-               } else {
-                       /* Default mode policy of interrupt is "Pause" */
-
-                       /* Send interrupt message */
-                       SLOG(LOG_DEBUG, tts_tag(), "[Server] Old uid(%u) will be interrupted into 'Pause' state ", current_uid);
-
-                       /* pause player */
-                       if (0 != ttsd_server_pause(current_uid)) {
-                               SLOG(LOG_WARN, tts_tag(), "[Server ERROR] Fail to ttsd_server_pause() : uid (%u)", current_uid);
-                       }
-               }
-
-               __send_interrupt_client(current_uid);
+       int ret = __interrupt_player_by_policy(uid);
+       if (TTSD_ERROR_NONE != ret) {
+               SLOG(LOG_WARN, tts_tag(), "[Server] Current play mode is Interrupt. Cannot play default, screen reader, and noti modes.");
+               return ret;
        }
 
        /* Change current play */
@@ -1271,7 +1249,6 @@ int ttsd_server_play_pcm(unsigned int uid)
                return TTSD_ERROR_OPERATION_FAILED;
        }
 
-       int ret = TTSD_ERROR_NONE;
        if (APP_STATE_PAUSED == state) {
                SLOG(LOG_DEBUG, tts_tag(), "[Server] uid(%u) is 'Pause' state : resume player", uid);
                ret = ttsd_player_resume(uid);