From 968b5632b01b56912284a6955e16d2c75245c788 Mon Sep 17 00:00:00 2001 From: sooyeon Date: Wed, 14 Aug 2024 20:01:14 +0900 Subject: [PATCH] Move creating AudioStream instance when playing tts is requested - Issue: At booting time, there is a case that focus server is not ready. Because focus server is not ready, initializing TTS engine is delayed with blocking a main thread. - Solution: Initializing AudioStream is moved to be invoked when playing TTS is requested. Change-Id: I141b9a1e7f2835675f625c778968d9308ec3c121 Signed-off-by: sooyeon --- server/ttsd_player.cpp | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/server/ttsd_player.cpp b/server/ttsd_player.cpp index 40ffa375..8b6ad269 100644 --- a/server/ttsd_player.cpp +++ b/server/ttsd_player.cpp @@ -132,11 +132,30 @@ static void write_buffer_dump_file(const void* buffer, size_t length) pthread_mutex_unlock(&g_buf_save_mutex); } +static int check_and_create_audio_stream(void) +{ + if (nullptr != g_audio_stream) + return 0; + + SLOG(LOG_ERROR, tts_tag(), "[BG] g_audio_stream is null. Create a new instance"); + g_audio_stream = new AudioStream(); + if (nullptr == g_audio_stream) { + SLOG(LOG_ERROR, tts_tag(), "[BG] Fail to allocate memory."); + return -1; + } + + return 0; +} + static void set_policy_for_playing(unsigned int uid) { /* In case of DEFAULT and INTERRUPT mode, acquire sound focus */ ttsd_mode_e mode = ttsd_data_get_mode(uid); SLOG(LOG_INFO, tts_tag(), "[Player INFO] Current uid(%d)'s mode(%d)", uid, (int)mode); + + if (-1 == check_and_create_audio_stream()) + return; + if (TTSD_MODE_DEFAULT == mode || TTSD_MODE_INTERRUPT == mode) g_audio_stream->acquireSoundFocus(); @@ -152,7 +171,8 @@ static void unset_policy_for_playing(unsigned int uid) /* In case of DEFAULT and INTERRUPT mode, release sound focus */ ttsd_mode_e mode = ttsd_data_get_mode(uid); SLOG(LOG_INFO, tts_tag(), "[Player INFO] Current uid(%d)'s mode(%d)", uid, (int)mode); - if (TTSD_MODE_DEFAULT == mode || TTSD_MODE_INTERRUPT == mode) + + if ((TTSD_MODE_DEFAULT == mode || TTSD_MODE_INTERRUPT == mode) && nullptr != g_audio_stream) g_audio_stream->releaseSoundFocus(); g_background_volume->recoverVolumeRatio(); @@ -209,6 +229,9 @@ static int notify_utterance_completed_event(unsigned int uid, int utt_id) static int play_sound_data(PlayerThread* player, unsigned int uid, sound_data_s* sound_data) { + if (-1 == check_and_create_audio_stream()) + return TTSD_ERROR_OUT_OF_MEMORY; + if (TTSD_ERROR_NONE != g_audio_stream->setAudioFormat(sound_data->audio_type, sound_data->rate)) { SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to create audio out"); return TTSD_ERROR_OPERATION_FAILED; @@ -411,7 +434,6 @@ 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(); g_player_thread = new PlayerThread(play_utterance_cb); g_is_set_policy = false; @@ -600,6 +622,9 @@ int ttsd_player_wait_to_play(unsigned int uid) return TTSD_ERROR_OPERATION_FAILED; } + if (-1 == check_and_create_audio_stream()) + return TTSD_ERROR_OUT_OF_MEMORY; + SLOG(LOG_INFO, tts_tag(), "[Player INFO] wait to play (%u)", uid); g_audio_stream->waitForPlay(); -- 2.34.1