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"
22 #include <bundle_internal.h>
26 #include <app_manager.h>
27 #include <system_info.h>
31 static int g_feature_enabled = -1;
32 static bool g_is_terminated = false;
40 static bool __is_feature_enabled()
42 if (1 == g_feature_enabled) {
46 if (0 == g_feature_enabled) {
47 SLOG(LOG_ERROR, tts_tag(), "[ERROR] TTS feature NOT supported");
51 bool tts_supported = false;
52 if (SYSTEM_INFO_ERROR_NONE != system_info_get_platform_bool(TTS_FEATURE_PATH, &tts_supported)) {
53 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to get feature value");
57 if (false == tts_supported) {
58 SLOG(LOG_ERROR, tts_tag(), "[ERROR] TTS feature NOT supported");
59 g_feature_enabled = 0;
63 g_feature_enabled = 1;
67 static bool __is_default_engine()
70 engine = vconf_get_str(VCONFKEY_TTS_ENGINE_DEFAULT);
72 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get sting for vc engine");
76 char appid[1024] = {'\0', };
77 if (0 != aul_app_get_appid_bypid(getpid(), appid, sizeof(appid) - 1)) {
78 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get callee appid by pid");
81 SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS Default Engine(%s), appId(%s)", engine, appid);
82 if (0 == strncmp(engine, appid, strlen(engine))) {
90 static void __engine_changed_cb(keynode_t* key, void* data)
92 SLOG(LOG_INFO, tts_tag(), "[INFO] TTS engine vconfkey is changed");
94 /* If a new TTS engine is different from the current engine, call ttse_terminate() */
95 if (FALSE == __is_default_engine()) {
96 SLOG(LOG_WARN, tts_tag(), "[WARNING] TTS engine is changed. Please call ttse_terminate()");
98 ecore_main_loop_quit();
100 SLOG(LOG_INFO, tts_tag(), "[INFO] A new TTS engine is same as the current engine.");
106 static bool __is_test_app()
109 int ret = app_manager_get_app_id(getpid(), &appid);
110 if (APP_MANAGER_ERROR_NONE != ret || NULL == appid) {
113 SLOG(LOG_INFO, tts_tag(), "[INFO] app id (%s)", appid);
115 bool is_test_app = false;
116 if (0 == strncmp(appid, "org.tizen.tts-native-itc", 32) || 0 == strncmp(appid, "org.tizen.tts-native-utc", 32)) {
117 SLOG(LOG_INFO, tts_tag(), "[INFO] Test mode is on");
125 int ttse_main(int argc, char** argv, ttse_request_callback_s *callback)
127 g_is_terminated = false;
130 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to initialize Ecore");
131 return TTSE_ERROR_OPERATION_FAILED;
134 int ret = ttsd_initialize(callback);
136 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to initialize");
141 if (TRUE == __is_default_engine()) {
142 if (0 != ttsd_ipc_open_connection()) {
143 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open ipc connection");
145 return TTSE_ERROR_OPERATION_FAILED;
149 /* If a new TTS engine is different from the current engine, call ttse_terminate() */
150 if (FALSE == __is_default_engine() && !__is_test_app()) {
151 SLOG(LOG_WARN, tts_tag(), "[WARNING] Before beginning main loop of TTS engine, default TTS engine is changed. Please call ttse_terminate()");
153 return TTSE_ERROR_OPERATION_FAILED;
156 /* Register vconfkey callback to detect engine change */
157 vconf_notify_key_changed(TTS_ENGINE_DB_DEFAULT, __engine_changed_cb, NULL);
159 SLOG(LOG_DEBUG, tts_tag(), "@@@");
161 return TTSE_ERROR_NONE;
164 int ttse_terminate(void)
166 if (true == g_is_terminated) {
167 SLOG(LOG_INFO, tts_tag(), "[INFO] ttse_terminate() is already invoked.");
168 return TTSE_ERROR_NONE;
171 int ret = ttsd_terminate();
172 if (TTSD_ERROR_NONE != ret) {
173 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_terminate(). ret(%d)", ret);
174 return TTSE_ERROR_OPERATION_FAILED;
177 /* Unregister vconfkey callback */
178 vconf_ignore_key_changed(TTS_ENGINE_DB_DEFAULT, __engine_changed_cb);
180 g_is_terminated = true;
181 return TTSE_ERROR_NONE;
184 int ttse_get_speed_range(int* min, int* normal, int* max)
186 if (NULL == min || NULL == normal || NULL == max) {
187 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Input parameter is null");
188 return TTSE_ERROR_INVALID_PARAMETER;
191 *min = TTS_SPEED_MIN;
192 *normal = TTS_SPEED_NORMAL;
193 *max = TTS_SPEED_MAX;
198 int ttse_get_pitch_range(int* min, int* normal, int* max)
200 if (NULL == min || NULL == normal || NULL == max) {
201 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Input parameter is null");
202 return TTSE_ERROR_INVALID_PARAMETER;
205 *min = TTS_PITCH_MIN;
206 *normal = TTS_PITCH_NORMAL;
207 *max = TTS_PITCH_MAX;
212 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)
217 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Input parameter is null");
220 ret = ttsd_send_result(event, data, data_size, audio_type, rate, user_data);
223 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to send result");
229 int ttse_send_error(ttse_error_e error, const char* msg)
233 ret = ttsd_send_error(error, msg);
236 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to send error");
242 int ttse_set_private_data_set_cb(ttse_private_data_set_cb callback_func)
244 if (NULL == callback_func) {
245 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Invalid parameter");
246 return TTSE_ERROR_INVALID_PARAMETER;
249 int ret = ttsd_set_private_data_set_cb(callback_func);
252 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to set private data set cb");
258 int ttse_set_private_data_requested_cb(ttse_private_data_requested_cb callback_func)
260 if (NULL == callback_func) {
261 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Invalid parameter");
262 return TTSE_ERROR_INVALID_PARAMETER;
265 int ret = ttsd_set_private_data_requested_cb(callback_func);
268 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to set private data requested cb");
274 int ttse_get_activated_mode(int* activated_mode)
276 if (false == __is_feature_enabled()) {
277 return TTSE_ERROR_NOT_SUPPORTED;
280 if (NULL == activated_mode) {
281 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Invalid parameter");
282 return TTSE_ERROR_INVALID_PARAMETER;
285 if (g_is_terminated) {
286 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Service engine is terminated.");
287 return TTSE_ERROR_INVALID_STATE;
290 int ret = ttsd_get_activated_mode(activated_mode);
291 if (TTSD_ERROR_NONE != ret) {
292 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to get activated mode. ret(%d/%s)", ret, get_error_message(ret));
298 int ttse_set_activated_mode_changed_cb(ttse_activated_mode_changed_cb callback)
300 if (false == __is_feature_enabled()) {
301 return TTSE_ERROR_NOT_SUPPORTED;
304 if (NULL == callback) {
305 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Invalid parameter");
306 return TTSE_ERROR_INVALID_PARAMETER;
309 if (g_is_terminated) {
310 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Service engine is terminated.");
311 return TTSE_ERROR_INVALID_STATE;
314 int ret = ttsd_set_activated_mode_changed_cb(callback);
315 if (TTSD_ERROR_NONE != ret) {
316 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to set activated mode changed cb. ret(%d/%s)", ret, get_error_message(ret));