2 * Copyright (c) 2011-2016 Samsung Electronics Co., Ltd All Rights Reserved
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 * http://www.apache.org/licenses/LICENSE-2.0
7 * Unless required by applicable law or agreed to in writing, software
8 * distributed under the License is distributed on an "AS IS" BASIS,
9 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 * See the License for the specific language governing permissions and
11 * limitations under the License.
15 #include "ttsd_main.h"
16 #include "ttsd_server.h"
17 #include "ttsd_dbus.h"
24 #include <app_manager.h>
25 #include <system_info.h>
29 static int g_feature_enabled = -1;
30 static bool g_is_terminated = false;
31 static bool g_is_started = false;
39 static bool is_feature_enabled()
41 if (1 == g_feature_enabled) {
45 if (0 == g_feature_enabled) {
46 SLOG(LOG_ERROR, tts_tag(), "[ERROR] TTS feature NOT supported");
50 bool tts_supported = false;
51 if (SYSTEM_INFO_ERROR_NONE != system_info_get_platform_bool(TTS_FEATURE_PATH, &tts_supported)) {
52 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to get feature value");
56 if (false == tts_supported) {
57 SLOG(LOG_ERROR, tts_tag(), "[ERROR] TTS feature NOT supported");
58 g_feature_enabled = 0;
62 g_feature_enabled = 1;
66 static bool __is_default_engine()
69 engine = vconf_get_str(VCONFKEY_TTS_ENGINE_DEFAULT);
71 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get sting for vc engine");
75 char appid[1024] = {'\0', };
76 if (0 != aul_app_get_appid_bypid(getpid(), appid, sizeof(appid) - 1)) {
77 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get callee appid by pid");
80 SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS Default Engine(%s), appId(%s)", engine, appid);
81 if (0 == strncmp(engine, appid, strlen(engine))) {
89 static void __engine_changed_cb(keynode_t* key, void* data)
91 SLOG(LOG_INFO, tts_tag(), "[INFO] TTS engine vconfkey is changed");
93 /* If a new TTS engine is different from the current engine, call ttse_terminate() */
94 if (FALSE == __is_default_engine()) {
95 SLOG(LOG_WARN, tts_tag(), "[WARNING] TTS engine is changed. Please call ttse_terminate()");
97 ecore_main_loop_quit();
99 SLOG(LOG_INFO, tts_tag(), "[INFO] A new TTS engine is same as the current engine.");
105 static bool __is_test_app()
108 int ret = app_manager_get_app_id(getpid(), &appid);
109 if (APP_MANAGER_ERROR_NONE != ret || NULL == appid) {
112 SLOG(LOG_INFO, tts_tag(), "[INFO] app id (%s)", appid);
114 bool is_test_app = false;
115 if (0 == strncmp(appid, "org.tizen.tts-native-itc", 32) || 0 == strncmp(appid, "org.tizen.tts-native-utc", 32)) {
116 SLOG(LOG_INFO, tts_tag(), "[INFO] Test mode is on");
124 int ttse_send_personal_voice(const char* language, const char* unique_id, const char* display_name, const char* device_name, void* user_data)
126 SLOG(LOG_INFO, tts_tag(), "[INFO] TTS Engine send personal voices list : language(%s), unique_id(%s), display_name(%s), device_name(%s)", language, unique_id, display_name, device_name);
128 int ret = ttsd_send_personal_voice(language, unique_id, display_name, device_name, NULL);
130 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to send personal voice");
133 return TTSE_ERROR_NONE;
136 int ttse_main(int argc, char** argv, ttse_request_callback_s *callback)
138 if (false == is_feature_enabled()) {
139 return TTSE_ERROR_NOT_SUPPORTED;
142 g_is_terminated = false;
145 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to initialize Ecore");
146 return TTSE_ERROR_OPERATION_FAILED;
149 int ret = ttsd_initialize(callback);
151 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to initialize");
156 if (TRUE == __is_default_engine()) {
157 if (0 != ttsd_ipc_open_connection()) {
158 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open ipc connection");
160 return TTSE_ERROR_OPERATION_FAILED;
164 /* If a new TTS engine is different from the current engine, call ttse_terminate() */
165 if (FALSE == __is_default_engine() && !__is_test_app()) {
166 SLOG(LOG_WARN, tts_tag(), "[WARNING] Before beginning main loop of TTS engine, default TTS engine is changed. Please call ttse_terminate()");
168 return TTSE_ERROR_OPERATION_FAILED;
171 /* Register vconfkey callback to detect engine change */
172 vconf_notify_key_changed(TTS_ENGINE_DB_DEFAULT, __engine_changed_cb, NULL);
176 SLOG(LOG_DEBUG, tts_tag(), "@@@");
178 return TTSE_ERROR_NONE;
181 int ttse_terminate(void)
183 if (false == is_feature_enabled()) {
184 return TTSE_ERROR_NOT_SUPPORTED;
187 if (true == g_is_terminated) {
188 SLOG(LOG_INFO, tts_tag(), "[INFO] ttse_terminate() is already invoked.");
189 return TTSE_ERROR_NONE;
192 int ret = ttsd_terminate();
193 if (TTSD_ERROR_NONE != ret) {
194 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_terminate(). ret(%d)", ret);
195 return TTSE_ERROR_OPERATION_FAILED;
198 /* Unregister vconfkey callback */
199 vconf_ignore_key_changed(TTS_ENGINE_DB_DEFAULT, __engine_changed_cb);
201 g_is_terminated = true;
202 g_is_started = false;
203 return TTSE_ERROR_NONE;
206 int ttse_get_speed_range(int* min, int* normal, int* max)
208 if (false == is_feature_enabled()) {
209 return TTSE_ERROR_NOT_SUPPORTED;
212 if (NULL == min || NULL == normal || NULL == max) {
213 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Input parameter is null");
214 return TTSE_ERROR_INVALID_PARAMETER;
217 *min = TTS_SPEED_MIN;
218 *normal = TTS_SPEED_NORMAL;
219 *max = TTS_SPEED_MAX;
224 int ttse_get_pitch_range(int* min, int* normal, int* max)
226 if (false == is_feature_enabled()) {
227 return TTSE_ERROR_NOT_SUPPORTED;
230 if (NULL == min || NULL == normal || NULL == max) {
231 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Input parameter is null");
232 return TTSE_ERROR_INVALID_PARAMETER;
235 *min = TTS_PITCH_MIN;
236 *normal = TTS_PITCH_NORMAL;
237 *max = TTS_PITCH_MAX;
242 int ttse_send_result(ttse_result_event_e event, const void* data, unsigned int data_size, ttse_audio_type_e audio_type, int rate, void* user_data)
244 if (false == is_feature_enabled()) {
245 return TTSE_ERROR_NOT_SUPPORTED;
251 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Input parameter is null");
254 ret = ttsd_send_result(event, data, data_size, audio_type, rate, user_data);
257 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to send result");
263 int ttse_send_error(ttse_error_e error, const char* msg)
265 if (false == is_feature_enabled()) {
266 return TTSE_ERROR_NOT_SUPPORTED;
271 ret = ttsd_send_error(error, msg);
274 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to send error");
280 int ttse_set_private_data_set_cb(ttse_private_data_set_cb callback_func)
282 if (false == is_feature_enabled()) {
283 return TTSE_ERROR_NOT_SUPPORTED;
286 if (NULL == callback_func) {
287 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Invalid parameter");
288 return TTSE_ERROR_INVALID_PARAMETER;
291 int ret = ttsd_set_private_data_set_cb(callback_func);
294 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to set private data set cb");
300 int ttse_set_private_data_requested_cb(ttse_private_data_requested_cb callback_func)
302 if (false == is_feature_enabled()) {
303 return TTSE_ERROR_NOT_SUPPORTED;
306 if (NULL == callback_func) {
307 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Invalid parameter");
308 return TTSE_ERROR_INVALID_PARAMETER;
311 int ret = ttsd_set_private_data_requested_cb(callback_func);
314 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to set private data requested cb");
320 int ttse_get_activated_mode(int* activated_mode)
322 if (false == is_feature_enabled()) {
323 return TTSE_ERROR_NOT_SUPPORTED;
326 if (NULL == activated_mode) {
327 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Invalid parameter");
328 return TTSE_ERROR_INVALID_PARAMETER;
331 if (false == g_is_started) {
332 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Service engine is not started.");
333 return TTSE_ERROR_INVALID_STATE;
336 int ret = ttsd_get_activated_mode(activated_mode);
337 if (TTSD_ERROR_NONE != ret) {
338 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to get activated mode. ret(%d/%s)", ret, get_error_message(ret));
344 int ttse_set_activated_mode_changed_cb(ttse_activated_mode_changed_cb callback)
346 if (false == is_feature_enabled()) {
347 return TTSE_ERROR_NOT_SUPPORTED;
350 if (NULL == callback) {
351 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Invalid parameter");
352 return TTSE_ERROR_INVALID_PARAMETER;
355 if (false == g_is_started) {
356 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Service engine is not started.");
357 return TTSE_ERROR_INVALID_STATE;
360 int ret = ttsd_set_activated_mode_changed_cb(callback);
361 if (TTSD_ERROR_NONE != ret) {
362 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to set activated mode changed cb. ret(%d/%s)", ret, get_error_message(ret));
368 int ttse_set_personal_tts_id_set_cb(ttse_set_personal_tts_id_cb callback)
370 if (false == is_feature_enabled()) {
371 return TTSE_ERROR_NOT_SUPPORTED;
374 if (NULL == callback) {
375 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Invalid parameter");
376 return TTSE_ERROR_INVALID_PARAMETER;
379 if (false == g_is_started) {
380 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Service engine is not started.");
381 return TTSE_ERROR_INVALID_STATE;
384 int ret = ttsd_set_personal_tts_id_set_cb(callback);
385 if (TTSD_ERROR_NONE != ret) {
386 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to set personal tts id set cb. ret(%d/%s)", ret, get_error_message(ret));