729f4daee0521f36de45053dad7e737c117344e9
[platform/core/uifw/tts.git] / server / ttse.c
1 /*
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.
12 */
13
14
15 #include "ttsd_main.h"
16 #include "ttsd_server.h"
17 #include "ttsd_dbus.h"
18 #include "ttsd_ipc.h"
19
20 #include <aul.h>
21 #include <bundle.h>
22 #include <bundle_internal.h>
23 #include <dlog.h>
24 #include <Ecore.h>
25 #include <vconf.h>
26 #include <app_manager.h>
27 #include <system_info.h>
28
29 #include "ttse.h"
30
31 static int g_feature_enabled = -1;
32 static bool g_is_terminated = false;
33
34
35 const char* tts_tag()
36 {
37         return "ttsd";
38 }
39
40 static bool __is_feature_enabled()
41 {
42         if (1 == g_feature_enabled) {
43                 return true;
44         }
45
46         if (0 == g_feature_enabled) {
47                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] TTS feature NOT supported");
48                 return false;
49         }
50
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");
54                 return false;
55         }
56
57         if (false == tts_supported) {
58                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] TTS feature NOT supported");
59                 g_feature_enabled = 0;
60                 return false;
61         }
62
63         g_feature_enabled = 1;
64         return true;
65 }
66
67 static bool __is_default_engine()
68 {
69         char* engine = NULL;
70         engine = vconf_get_str(VCONFKEY_TTS_ENGINE_DEFAULT);
71         if (NULL == engine) {
72                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get sting for vc engine");
73                 return FALSE;
74         }
75
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");
79         }
80
81         SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS Default Engine(%s), appId(%s)", engine, appid);
82         if (0 == strncmp(engine, appid, strlen(engine))) {
83                 free(engine);
84                 return TRUE;
85         }
86         free(engine);
87         return FALSE;
88 }
89
90 static void __engine_changed_cb(keynode_t* key, void* data)
91 {
92         SLOG(LOG_INFO, tts_tag(), "[INFO] TTS engine vconfkey is changed");
93
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()");
97                 ttse_terminate();
98                 ecore_main_loop_quit();
99         } else {
100                 SLOG(LOG_INFO, tts_tag(), "[INFO] A new TTS engine is same as the current engine.");
101         }
102
103         return;
104 }
105
106 static bool __is_test_app()
107 {
108         char* appid = NULL;
109         int ret = app_manager_get_app_id(getpid(), &appid);
110         if (APP_MANAGER_ERROR_NONE != ret || NULL == appid) {
111                 return false;
112         }
113         SLOG(LOG_INFO, tts_tag(), "[INFO] app id (%s)", appid);
114
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");
118                 is_test_app = true;
119         }
120
121         free(appid);
122         return is_test_app;
123 }
124
125 int ttse_main(int argc, char** argv, ttse_request_callback_s *callback)
126 {
127         g_is_terminated = false;
128
129         if (!ecore_init()) {
130                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to initialize Ecore");
131                 return TTSE_ERROR_OPERATION_FAILED;
132         }
133
134         int ret = ttsd_initialize(callback);
135         if (0 != ret) {
136                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to initialize");
137                 ecore_shutdown();
138                 return ret;
139         }
140
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");
144                         ecore_shutdown();
145                         return TTSE_ERROR_OPERATION_FAILED;
146                 }
147         }
148
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()");
152                 ttse_terminate();
153                 return TTSE_ERROR_OPERATION_FAILED;
154         }
155
156         /* Register vconfkey callback to detect engine change */
157         vconf_notify_key_changed(TTS_ENGINE_DB_DEFAULT, __engine_changed_cb, NULL);
158
159         SLOG(LOG_DEBUG, tts_tag(), "@@@");
160
161         return TTSE_ERROR_NONE;
162 }
163
164 int ttse_terminate(void)
165 {
166         if (true == g_is_terminated) {
167                 SLOG(LOG_INFO, tts_tag(), "[INFO] ttse_terminate() is already invoked.");
168                 return TTSE_ERROR_NONE;
169         }
170
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;
175         }
176
177         /* Unregister vconfkey callback */
178         vconf_ignore_key_changed(TTS_ENGINE_DB_DEFAULT, __engine_changed_cb);
179
180         g_is_terminated = true;
181         return TTSE_ERROR_NONE;
182 }
183
184 int ttse_get_speed_range(int* min, int* normal, int* max)
185 {
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;
189         }
190
191         *min = TTS_SPEED_MIN;
192         *normal = TTS_SPEED_NORMAL;
193         *max = TTS_SPEED_MAX;
194
195         return 0;
196 }
197
198 int ttse_get_pitch_range(int* min, int* normal, int* max)
199 {
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;
203         }
204
205         *min = TTS_PITCH_MIN;
206         *normal = TTS_PITCH_NORMAL;
207         *max = TTS_PITCH_MAX;
208
209         return 0;
210 }
211
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)
213 {
214         int ret;
215
216         if (NULL == data) {
217                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Input parameter is null");
218         }
219
220         ret = ttsd_send_result(event, data, data_size, audio_type, rate, user_data);
221
222         if (0 != ret) {
223                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to send result");
224         }
225
226         return ret;
227 }
228
229 int ttse_send_error(ttse_error_e error, const char* msg)
230 {
231         int ret;
232
233         ret = ttsd_send_error(error, msg);
234
235         if (0 != ret) {
236                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to send error");
237         }
238
239         return ret;
240 }
241
242 int ttse_set_private_data_set_cb(ttse_private_data_set_cb callback_func)
243 {
244         if (NULL == callback_func) {
245                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Invalid parameter");
246                 return TTSE_ERROR_INVALID_PARAMETER;
247         }
248
249         int ret = ttsd_set_private_data_set_cb(callback_func);
250
251         if (0 != ret) {
252                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to set private data set cb");
253         }
254
255         return ret;
256 }
257
258 int ttse_set_private_data_requested_cb(ttse_private_data_requested_cb callback_func)
259 {
260         if (NULL == callback_func) {
261                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Invalid parameter");
262                 return TTSE_ERROR_INVALID_PARAMETER;
263         }
264
265         int ret = ttsd_set_private_data_requested_cb(callback_func);
266
267         if (0 != ret) {
268                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to set private data requested cb");
269         }
270
271         return ret;
272 }
273
274 int ttse_get_activated_mode(int* activated_mode)
275 {
276         if (false == __is_feature_enabled()) {
277                 return TTSE_ERROR_NOT_SUPPORTED;
278         }
279
280         if (NULL == activated_mode) {
281                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Invalid parameter");
282                 return TTSE_ERROR_INVALID_PARAMETER;
283         }
284
285         if (g_is_terminated) {
286                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Service engine is terminated.");
287                 return TTSE_ERROR_INVALID_STATE;
288         }
289
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));
293         }
294
295         return ret;
296 }
297
298 int ttse_set_activated_mode_changed_cb(ttse_activated_mode_changed_cb callback)
299 {
300         if (false == __is_feature_enabled()) {
301                 return TTSE_ERROR_NOT_SUPPORTED;
302         }
303
304         if (NULL == callback) {
305                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Invalid parameter");
306                 return TTSE_ERROR_INVALID_PARAMETER;
307         }
308
309         if (g_is_terminated) {
310                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Service engine is terminated.");
311                 return TTSE_ERROR_INVALID_STATE;
312         }
313
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));
317         }
318
319         return ret;
320 }