Move creating AudioStream instance when playing tts is requested 93/316693/1
authorsooyeon <sooyeon.kim@samsung.com>
Wed, 14 Aug 2024 11:01:14 +0000 (20:01 +0900)
committerSooyeon Kim <sooyeon.kim@samsung.com>
Tue, 27 Aug 2024 04:44:51 +0000 (04:44 +0000)
- 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 <sooyeon.kim@samsung.com>
server/ttsd_player.cpp

index 40ffa3758dfdd5d2e6f68a44b35635daf3b1d085..8b6ad2698dcc32024cbe66190e19303558fa1a64 100644 (file)
@@ -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();