Add internal api for server TTS and error callback logic
[platform/core/uifw/tts.git] / client / tts.c
index b37afb9..09f3892 100644 (file)
@@ -1,5 +1,5 @@
 /*
-*  Copyright (c) 2011-2014 Samsung Electronics Co., Ltd All Rights Reserved
+*  Copyright (c) 2011-2016 Samsung Electronics Co., Ltd All Rights Reserved
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
@@ -11,6 +11,7 @@
 *  limitations under the License.
 */
 
+#include <app_manager.h>
 #include <dirent.h>
 #include <Ecore.h>
 #include <iconv.h>
@@ -82,6 +83,8 @@ static const char* __tts_get_error_code(tts_error_e err)
        case TTS_ERROR_ENGINE_NOT_FOUND:        return "TTS_ERROR_ENGINE_NOT_FOUND";
        case TTS_ERROR_OPERATION_FAILED:        return "TTS_ERROR_OPERATION_FAILED";
        case TTS_ERROR_AUDIO_POLICY_BLOCKED:    return "TTS_ERROR_AUDIO_POLICY_BLOCKED";
+       case TTS_ERROR_NOT_SUPPORTED_FEATURE:   return "TTS_ERROR_NOT_SUPPORTED_FEATURE";
+       case TTS_ERROR_SERVICE_RESET:           return "TTS_ERROR_SERVICE_RESET";
        default:
                return "Invalid error code";
        }
@@ -98,7 +101,7 @@ static int __tts_convert_config_error_code(tts_config_error_e code)
        if (code == TTS_CONFIG_ERROR_INVALID_VOICE)             return TTS_ERROR_INVALID_VOICE;
        if (code == TTS_CONFIG_ERROR_ENGINE_NOT_FOUND)          return TTS_ERROR_ENGINE_NOT_FOUND;
        if (code == TTS_CONFIG_ERROR_OPERATION_FAILED)          return TTS_ERROR_OPERATION_FAILED;
-       if (code == TTS_CONFIG_ERROR_NOT_SUPPORTED_FEATURE)     return TTS_ERROR_OPERATION_FAILED;
+       if (code == TTS_CONFIG_ERROR_NOT_SUPPORTED_FEATURE)     return TTS_ERROR_NOT_SUPPORTED_FEATURE;
 
        return code;
 }
@@ -134,6 +137,30 @@ void __tts_config_voice_changed_cb(const char* before_lang, int before_voice_typ
        return;
 }
 
+void _tts_config_engine_changed_cb(const char* engine_id, const char* setting, const char* language, int voice_type, bool auto_voice, bool need_credential, void* user_data)
+{
+       tts_h tts = (tts_h)user_data;
+
+       tts_client_s* client = tts_client_get(tts);
+       if (NULL == client) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[WARNING] A handle is not valid");
+               return;
+       }
+
+       if (NULL != engine_id)  SLOG(LOG_DEBUG, TAG_TTSC, "Engine id(%s)", engine_id);
+       if (NULL != setting)    SLOG(LOG_DEBUG, TAG_TTSC, "Engine setting(%s)", setting);
+       if (NULL != language)   SLOG(LOG_DEBUG, TAG_TTSC, "Language(%s)", language);
+       SLOG(LOG_DEBUG, TAG_TTSC, "Voice type(%d), Auto voice(%s), Credential(%s)", voice_type, auto_voice ? "on" : "off", need_credential ? "need" : "no need");
+
+       /* call callback function */
+       if (NULL != client->engine_changed_cb) {
+               client->engine_changed_cb(tts, engine_id, language, voice_type, need_credential, client->engine_changed_user_data);
+       } else {
+               SLOG(LOG_WARN, TAG_TTSC, "No registered callback function for changed engine");
+       }
+       return;
+}
+
 int tts_create(tts_h* tts)
 {
        if (0 != __tts_get_feature_enabled()) {
@@ -175,7 +202,7 @@ int tts_create(tts_h* tts)
                return __tts_convert_config_error_code(ret);
        }
 
-       ret = tts_config_mgr_set_callback(client->uid, NULL, __tts_config_voice_changed_cb, NULL, NULL, NULL);
+       ret = tts_config_mgr_set_callback(client->uid, _tts_config_engine_changed_cb, __tts_config_voice_changed_cb, NULL, NULL, NULL);
        if (0 != ret) {
                SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to set config changed : %d", ret);
                tts_client_destroy(*tts);
@@ -377,6 +404,116 @@ int tts_get_mode(tts_h tts, tts_mode_e* mode)
        return TTS_ERROR_NONE;
 }
 
+int tts_set_credential(tts_h tts, const char* credential)
+{
+       if (0 != __tts_get_feature_enabled()) {
+               return TTS_ERROR_NOT_SUPPORTED;
+       }
+
+       if (NULL == tts || NULL == credential) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input parameter is null");
+               return TTS_ERROR_INVALID_PARAMETER;
+       }
+
+       tts_client_s* client = tts_client_get(tts);
+
+       if (NULL == client) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Get state : A handle is not valid");
+               return TTS_ERROR_INVALID_PARAMETER;
+       }
+
+       if (TTS_STATE_CREATED != client->current_state && TTS_STATE_READY != client->current_state) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] The current state is invalid (%d).", client->current_state);
+               return TTS_ERROR_INVALID_STATE;
+       }
+
+       if (NULL != client->credential) {
+               free(client->credential);
+               client->credential = NULL;
+       }
+       client->credential = strdup(credential);
+
+       SLOG(LOG_DEBUG, TAG_TTSC, "=====");
+       SLOG(LOG_DEBUG, TAG_TTSC, " ");
+
+       return TTS_ERROR_NONE;
+}
+
+int tts_set_server_tts(tts_h tts, const char* credential)
+{
+       if (0 != __tts_get_feature_enabled()) {
+               return TTS_ERROR_NOT_SUPPORTED;
+       }
+
+       if (NULL == tts || NULL == credential) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input parameter is null");
+               return TTS_ERROR_INVALID_PARAMETER;
+       }
+
+       tts_client_s* client = tts_client_get(tts);
+
+       if (NULL == client) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Get state : A handle is not valid");
+               return TTS_ERROR_INVALID_PARAMETER;
+       }
+
+       if (TTS_STATE_CREATED != client->current_state && TTS_STATE_READY != client->current_state) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] The current state is invalid (%d).", client->current_state);
+               return TTS_ERROR_INVALID_STATE;
+       }
+
+       if (NULL != client->credential) {
+               free(client->credential);
+               client->credential = NULL;
+       }
+
+       client->credential = strdup(credential);
+       if (NULL == client->credential) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to allocate memory");
+               return TTS_ERROR_OUT_OF_MEMORY;
+       }
+
+       client->internal = true;
+
+       char* key = NULL;
+       if (NULL != credential)
+               key = strdup("EnableServerTTS");
+       else
+               key = strdup("DisableServerTTS");
+       if (NULL == key) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to allocate memory");
+               return TTS_ERROR_OUT_OF_MEMORY;
+       }
+
+       int pid = getpid();
+       char* appid = NULL;
+       int ret = app_manager_get_app_id(pid, &appid);
+       if (0 != ret) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to get appid, ret(%d), pid(%d), appid(%s)", ret, pid, appid);
+               free(key);
+               key = NULL;
+               return TTS_ERROR_OPERATION_FAILED;
+       }
+
+       ret = tts_set_private_data(tts, key, appid);
+       if (0 != ret) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to set private data, ret(%d), pid(%d), appid(%s)", ret, pid, appid);
+               free(key);
+               key = NULL;
+               return ret;
+       }
+
+       free(appid);
+       appid = NULL;
+       free(key);
+       key = NULL;
+
+       SLOG(LOG_DEBUG, TAG_TTSC, "=====");
+       SLOG(LOG_DEBUG, TAG_TTSC, " ");
+
+       return TTS_ERROR_NONE;
+}
+
 static Eina_Bool __tts_connect_daemon(void *data)
 {
        tts_h tts = (tts_h)data;
@@ -397,7 +534,9 @@ static Eina_Bool __tts_connect_daemon(void *data)
 
        /* do request initialize */
        int ret = -1;
-       ret = tts_dbus_request_initialize(client->uid);
+       bool credential_needed = false;
+
+       ret = tts_dbus_request_initialize(client->uid, &credential_needed);
 
        if (TTS_ERROR_ENGINE_NOT_FOUND == ret) {
                SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to initialize : %s", __tts_get_error_code(ret));
@@ -415,6 +554,8 @@ static Eina_Bool __tts_connect_daemon(void *data)
 
        } else {
                /* success to connect tts-daemon */
+               client->credential_needed = credential_needed;
+               SLOG(LOG_ERROR, TAG_TTSC, "Supported options : credential(%s)", credential_needed ? "need" : "no need");
        }
 
        client->conn_timer = NULL;
@@ -741,7 +882,7 @@ int tts_get_speed_range(tts_h tts, int* min, int* normal, int* max)
 
 int tts_get_error_message(tts_h tts, char** err_msg)
 {
-       if(0 != __tts_get_feature_enabled()) {
+       if (0 != __tts_get_feature_enabled()) {
                return TTS_ERROR_NOT_SUPPORTED;
        }
 
@@ -772,6 +913,8 @@ int tts_get_error_message(tts_h tts, char** err_msg)
 
 int tts_add_text(tts_h tts, const char* text, const char* language, int voice_type, int speed, int* utt_id)
 {
+       SLOG(LOG_DEBUG, TAG_TTSC, "[DEBUG] Add text: text(%s), language(%s), type(%d)", (NULL == text) ? "NULL" : text, (NULL == language) ? "NULL" : language, voice_type);
+
        if (0 != __tts_get_feature_enabled()) {
                return TTS_ERROR_NOT_SUPPORTED;
        }
@@ -828,6 +971,11 @@ int tts_add_text(tts_h tts, const char* text, const char* language, int voice_ty
                return TTS_ERROR_INVALID_STATE;
        }
 
+       if (true == client->credential_needed && NULL == client->credential) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Do not have app credential for this engine");
+               return TTS_ERROR_PERMISSION_DENIED;
+       }
+
        /* check valid utf8 */
        iconv_t *ict;
        ict = iconv_open("utf-8", "");
@@ -877,7 +1025,7 @@ int tts_add_text(tts_h tts, const char* text, const char* language, int voice_ty
        int ret = -1;
        int count = 0;
        while (0 != ret) {
-               ret = tts_dbus_request_add_text(client->uid, out_buf, temp, voice_type, speed, client->current_utt_id);
+               ret = tts_dbus_request_add_text(client->uid, out_buf, temp, voice_type, speed, client->current_utt_id, client->credential);
                if (0 != ret) {
                        if (TTS_ERROR_TIMED_OUT != ret) {
                                SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
@@ -886,7 +1034,7 @@ int tts_add_text(tts_h tts, const char* text, const char* language, int voice_ty
                                SLOG(LOG_WARN, TAG_TTSC, "[WARNING] retry add text : %s", __tts_get_error_code(ret));
                                usleep(10000);
                                count++;
-                               if (TTS_RETRY_COUNT == count) {
+                               if (TTS_RETRY_MIN_COUNT == count) {
                                        SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to request");
                                        break;
                                }
@@ -918,7 +1066,7 @@ static void __tts_play_async(void *data)
        int ret = -1;
        int count = 0;
        while (0 != ret) {
-               ret = tts_dbus_request_play(client->uid);
+               ret = tts_dbus_request_play(client->uid, client->credential);
                if (0 != ret) {
                        if (TTS_ERROR_TIMED_OUT != ret) {
                                SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
@@ -995,6 +1143,11 @@ int tts_play_async(tts_h tts)
                return TTS_ERROR_INVALID_STATE;
        }
 
+       if (true == client->credential_needed && NULL == client->credential) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Do not have app credential for this engine");
+               return TTS_ERROR_PERMISSION_DENIED;
+       }
+
        ecore_main_loop_thread_safe_call_async(__tts_play_async, (void*)tts);
 
        SLOG(LOG_DEBUG, TAG_TTSC, "=====");
@@ -1037,10 +1190,15 @@ int tts_play(tts_h tts)
                return TTS_ERROR_INVALID_STATE;
        }
 
+       if (true == client->credential_needed && NULL == client->credential) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Do not have app credential for this engine");
+               return TTS_ERROR_PERMISSION_DENIED;
+       }
+
        int ret = -1;
        int count = 0;
        while (0 != ret) {
-               ret = tts_dbus_request_play(client->uid);
+               ret = tts_dbus_request_play(client->uid, client->credential);
                if (0 != ret) {
                        if (TTS_ERROR_TIMED_OUT != ret) {
                                SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] result : %s", __tts_get_error_code(ret));
@@ -1427,7 +1585,7 @@ int tts_set_private_data(tts_h tts, const char* key, const char* data)
                SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null");
                return TTS_ERROR_INVALID_PARAMETER;
        }
-       
+
        if (NULL == key || NULL == data) {
                SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid parameter");
                return TTS_ERROR_INVALID_PARAMETER;
@@ -1445,9 +1603,9 @@ int tts_set_private_data(tts_h tts, const char* key, const char* data)
                return TTS_ERROR_INVALID_STATE;
        }
 
-       if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
-               SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
-               return TTS_ERROR_INVALID_STATE;
+       if (true != client->internal && (0 == strcmp(key, "EnableServerTTS") || 0 == strcmp(key, "DisableServerTTS"))) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] This is not an internal app");
+               return TTS_ERROR_INVALID_PARAMETER;
        }
 
        int ret = -1;
@@ -1488,7 +1646,7 @@ int tts_get_private_data(tts_h tts, const char* key, char** data)
                SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Input handle is null");
                return TTS_ERROR_INVALID_PARAMETER;
        }
-       
+
        if (NULL == key || NULL == data) {
                SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Invalid parameter");
                return TTS_ERROR_INVALID_PARAMETER;
@@ -1506,11 +1664,6 @@ int tts_get_private_data(tts_h tts, const char* key, char** data)
                return TTS_ERROR_INVALID_STATE;
        }
 
-       if (false == g_screen_reader && TTS_MODE_SCREEN_READER == client->mode) {
-               SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Screen reader option is NOT available. Ignore this request");
-               return TTS_ERROR_INVALID_STATE;
-       }
-
        int ret = -1;
        int count = 0;
        while (0 != ret) {
@@ -1531,6 +1684,11 @@ int tts_get_private_data(tts_h tts, const char* key, char** data)
                }
        }
 
+       if (0 == strncmp(*data, "NULL", strlen(*data))) {
+               free(*data);
+               *data = NULL;
+       }
+
        SLOG(LOG_DEBUG, TAG_TTSC, "=====");
        SLOG(LOG_DEBUG, TAG_TTSC, "");
 
@@ -1589,6 +1747,15 @@ int __tts_cb_error(int uid, tts_error_e reason, int utt_id, char* err_msg)
                SLOG(LOG_WARN, TAG_TTSC, "No registered callback function of error ");
        }
 
+       if (TTS_ERROR_SERVICE_RESET == reason) {
+               SLOG(LOG_WARN, TAG_TTSC, "[WARNING] Service Reset");
+
+               client->current_state = TTS_STATE_CREATED;
+               if (0 != tts_prepare(client->tts)) {
+                       SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Fail to prepare");
+               }
+       }
+
        return 0;
 }
 
@@ -2003,3 +2170,66 @@ int tts_unset_default_voice_changed_cb(tts_h tts)
 
        return 0;
 }
+
+int tts_set_engine_changed_cb(tts_h tts, tts_engine_changed_cb callback, void* user_data)
+{
+       if (0 != __tts_get_feature_enabled()) {
+               return TTS_ERROR_NOT_SUPPORTED;
+       }
+
+       if (NULL == tts || NULL == callback) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set engine changed cb : Input parameter is null");
+               return TTS_ERROR_INVALID_PARAMETER;
+       }
+
+       tts_client_s* client = tts_client_get(tts);
+
+       if (NULL == client) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set engine changed cb : A handle is not valid");
+               return TTS_ERROR_INVALID_PARAMETER;
+       }
+
+       if (TTS_STATE_CREATED != client->current_state) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Set engine changed cb : Current state is not 'Created'.");
+               return TTS_ERROR_INVALID_STATE;
+       }
+
+       client->engine_changed_cb = callback;
+       client->engine_changed_user_data = user_data;
+
+       SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Set engine changed cb");
+
+       return 0;
+}
+
+int tts_unset_engine_changed_cb(tts_h tts)
+{
+       if (0 != __tts_get_feature_enabled()) {
+               return TTS_ERROR_NOT_SUPPORTED;
+       }
+
+       if (NULL == tts) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset engine changed cb : Input parameter is null");
+               return TTS_ERROR_INVALID_PARAMETER;
+       }
+
+       tts_client_s* client = tts_client_get(tts);
+
+       if (NULL == client) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset engine changed cb : A handle is not valid");
+               return TTS_ERROR_INVALID_PARAMETER;
+       }
+
+       if (TTS_STATE_CREATED != client->current_state) {
+               SLOG(LOG_ERROR, TAG_TTSC, "[ERROR] Unset engine changed cb : Current state is not 'Created'.");
+               return TTS_ERROR_INVALID_STATE;
+       }
+
+       client->engine_changed_cb = NULL;
+       client->engine_changed_user_data = NULL;
+
+       SLOG(LOG_DEBUG, TAG_TTSC, "[SUCCESS] Unset engine changed cb");
+
+       return 0;
+}
+