Clean up mixing policy code 87/264187/3
authorSuyeon Hwang <stom.hwang@samsung.com>
Wed, 8 Sep 2021 01:02:36 +0000 (10:02 +0900)
committerSuyeon Hwang <stom.hwang@samsung.com>
Tue, 12 Oct 2021 05:36:19 +0000 (14:36 +0900)
To fix the policy, this patch refactors the code which handles the policy.
By this patch, readability of the code will be enhanced and it will help to fix the policy.

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

index df2c090c6922686ca4eb968c0f06e5e17ab886b4..6895d0cb015dde6bfec823abfc409352cf86ad8a 100644 (file)
@@ -54,6 +54,7 @@ typedef struct {
 #define FOCUS_SERVER_READY "/tmp/.sound_server_ready"
 
 static const intptr_t CHECK_TIMER_DELETE = 1;
+static const int EXTRA_INFO_LENGTH = 20;
 
 /* Sound buf save for test */
 /*
@@ -156,48 +157,63 @@ player_s* __player_get_item(int uid)
        return NULL;
 }
 
-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,
+static bool __is_focus_released_on_playing(sound_stream_focus_mask_e focus_mask, sound_stream_focus_state_e focus_state)
+{
+       if (NULL == g_playing_info) {
+               SLOG(LOG_INFO, tts_tag(), "[Player] No current player");
+               return false;
+       }
+
+       if (APP_STATE_PLAYING != g_playing_info->state || AUDIO_STATE_NONE == g_audio_state || AUDIO_STATE_READY == g_audio_state) {
+               SLOG(LOG_INFO, tts_tag(), "[Player] Audio is not played");
+               return false;
+       }
+
+       if (SOUND_STREAM_FOCUS_FOR_PLAYBACK != focus_mask || SOUND_STREAM_FOCUS_STATE_RELEASED != focus_state) {
+               SLOG(LOG_INFO, tts_tag(), "[Player] Playback focus is not released");
+               return false;
+       }
+
+       return true;
+}
+
+static 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_WARN, tts_tag(), "[Player] focus state changed to (%d) with reason(%d) and extra info(%s)", (int)focus_state, (int)reason_for_change, extra_info);
 
        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) and extra info(%s)", (int)focus_state, (int)reason_for_change, extra_info);
-
-       if ((AUDIO_STATE_PLAY == g_audio_state || AUDIO_STATE_WAIT_FOR_PLAYING == 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");
-                               return;
-                       }
+       if (false == __is_focus_released_on_playing(focus_mask, focus_state)) {
+               SLOG(LOG_INFO, tts_tag(), "[Player INFO] Playback focus is not released on playing");
+               return;
+       }
 
-                       if (APP_STATE_PLAYING == g_playing_info->state) {
-                               int uid = g_playing_info->uid;
+       if (TTSD_MODE_DEFAULT == ttsd_get_mode()) {
+               g_audio_state = AUDIO_STATE_READY;
+               int uid = g_playing_info->uid;
 
-                               if (0 != ttsd_player_pause(uid)) {
-                                       SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to pause the player");
-                                       return;
-                               }
+               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);
-                               if (pid <= 0) {
-                                       SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to get pid. uid(%d)", uid);
-                                       return;
-                               } else {
-                                       /* send message to client about changing state */
-                                       SLOG(LOG_INFO, tts_tag(), "[Player INFO] Player paused. pid(%d), uid(%d)", pid, uid);
-                                       ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
-                               }
-                       }
+               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(%d)", uid);
+                       return;
                } else {
-                       SLOG(LOG_DEBUG, tts_tag(), "[Player] Ignore focus state cb - mode(%d)", ttsd_get_mode());
+                       /* send message to client about changing state */
+                       SLOG(LOG_INFO, tts_tag(), "[Player INFO] Player paused. pid(%d), uid(%d)", pid, uid);
+                       ttsdc_ipc_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());
        }
 
 /*     if (AUDIO_STATE_READY == g_audio_state && focus_mask == SOUND_STREAM_FOCUS_FOR_PLAYBACK && SOUND_STREAM_FOCUS_STATE_ACQUIRED == focus_state) {
@@ -542,41 +558,32 @@ static void __unset_policy_for_playing()
        return;
 }
 
-int ttsd_player_check_current_playback_focus(bool *is_current_interrupt)
+static bool __does_interrupt_have_focus(sound_stream_focus_change_reason_e reason, int sound_behavior, char *extra_info)
 {
-       int ret;
-       ttsd_mode_e mode = ttsd_get_mode();
-
-       if (TTSD_MODE_INTERRUPT != mode) {
-               /* check the current playback focus */
-               sound_stream_focus_change_reason_e reason;
-               int sound_behavior = 0;
-               char *extra_info = NULL;
-
-               ret = sound_manager_get_current_playback_focus(&reason, &sound_behavior, &extra_info);
-
-               SLOG(LOG_DEBUG, tts_tag(), "[Player] current playback focus: extra_info(%s), reason(%d), sound_behavior(%d)", extra_info, reason, sound_behavior);
+       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 (SOUND_MANAGER_ERROR_NONE == ret && NULL != extra_info && 0 < strlen(extra_info)) {
-                       if (SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_INFORMATION == reason && 0 == strncmp(extra_info, "TTSD_MODE_INTERRUPT", strlen(extra_info))) {
-                               SLOG(LOG_DEBUG, tts_tag(), "[Player] The current focus in Interrupt. Cannot play the requested sound data");
-                               *is_current_interrupt = true;
+       if (NULL == extra_info || 0 >= strlen(extra_info) || 0 != strncmp(extra_info, "TTSD_MODE_INTERRUPT", EXTRA_INFO_LENGTH)) {
+               return false;
+       }
 
-                               free(extra_info);
-                               extra_info = NULL;
+       return true;
+}
 
-                               return TTSD_ERROR_NONE;
-                       }
-               }
+int ttsd_player_check_current_playback_focus(bool *is_current_interrupt)
+{
+       *is_current_interrupt = false;
 
-               if (NULL != extra_info) {
-                       free(extra_info);
-                       extra_info = NULL;
-               }
+       sound_stream_focus_change_reason_e reason;
+       int sound_behavior = 0;
+       char *extra_info = NULL;
+       if (SOUND_MANAGER_ERROR_NONE == sound_manager_get_current_playback_focus(&reason, &sound_behavior, &extra_info)) {
+               *is_current_interrupt = __does_interrupt_have_focus(reason, sound_behavior, extra_info);
        }
 
-       *is_current_interrupt = false;
-
+       free(extra_info);
        return TTSD_ERROR_NONE;
 }