From 0522f023d6fc1e685b46f2c02e26e5c2f26fe60c Mon Sep 17 00:00:00 2001 From: Suyeon Hwang Date: Wed, 8 Sep 2021 10:02:36 +0900 Subject: [PATCH] Clean up mixing policy code 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 --- server/ttsd_player.c | 119 +++++++++++++++++++++++-------------------- 1 file changed, 63 insertions(+), 56 deletions(-) diff --git a/server/ttsd_player.c b/server/ttsd_player.c index df2c090c..6895d0cb 100644 --- a/server/ttsd_player.c +++ b/server/ttsd_player.c @@ -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; } -- 2.34.1