Add checking personal voice and create speak data with personal voice id
[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 <dlog.h>
22 #include <Ecore.h>
23 #include <vconf.h>
24 #include <app_manager.h>
25 #include <system_info.h>
26
27 #include "ttse.h"
28
29 static int g_feature_enabled = -1;
30 static bool g_is_terminated = false;
31 static bool g_is_started = false;
32
33
34 const char* tts_tag()
35 {
36         return "ttsd";
37 }
38
39 static bool is_feature_enabled()
40 {
41         if (1 == g_feature_enabled) {
42                 return true;
43         }
44
45         if (0 == g_feature_enabled) {
46                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] TTS feature NOT supported");
47                 return false;
48         }
49
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");
53                 return false;
54         }
55
56         if (false == tts_supported) {
57                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] TTS feature NOT supported");
58                 g_feature_enabled = 0;
59                 return false;
60         }
61
62         g_feature_enabled = 1;
63         return true;
64 }
65
66 static bool __is_default_engine()
67 {
68         char* engine = NULL;
69         engine = vconf_get_str(VCONFKEY_TTS_ENGINE_DEFAULT);
70         if (NULL == engine) {
71                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get sting for vc engine");
72                 return FALSE;
73         }
74
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");
78         }
79
80         SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS Default Engine(%s), appId(%s)", engine, appid);
81         if (0 == strncmp(engine, appid, strlen(engine))) {
82                 free(engine);
83                 return TRUE;
84         }
85         free(engine);
86         return FALSE;
87 }
88
89 static void __engine_changed_cb(keynode_t* key, void* data)
90 {
91         SLOG(LOG_INFO, tts_tag(), "[INFO] TTS engine vconfkey is changed");
92
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()");
96                 ttse_terminate();
97                 ecore_main_loop_quit();
98         } else {
99                 SLOG(LOG_INFO, tts_tag(), "[INFO] A new TTS engine is same as the current engine.");
100         }
101
102         return;
103 }
104
105 static bool __is_test_app()
106 {
107         char* appid = NULL;
108         int ret = app_manager_get_app_id(getpid(), &appid);
109         if (APP_MANAGER_ERROR_NONE != ret || NULL == appid) {
110                 return false;
111         }
112         SLOG(LOG_INFO, tts_tag(), "[INFO] app id (%s)", appid);
113
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");
117                 is_test_app = true;
118         }
119
120         free(appid);
121         return is_test_app;
122 }
123
124 int ttse_send_personal_voice(const char* language, const char* unique_id, const char* display_name, const char* device_name, void* user_data)
125 {
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);
127
128         int ret = ttsd_send_personal_voice(language, unique_id, display_name, device_name, NULL);
129         if (0 != ret) {
130                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to send personal voice");
131         }
132
133         return TTSE_ERROR_NONE;
134 }
135
136 int ttse_main(int argc, char** argv, ttse_request_callback_s *callback)
137 {
138         if (false == is_feature_enabled()) {
139                 return TTSE_ERROR_NOT_SUPPORTED;
140         }
141
142         g_is_terminated = false;
143
144         if (!ecore_init()) {
145                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to initialize Ecore");
146                 return TTSE_ERROR_OPERATION_FAILED;
147         }
148
149         int ret = ttsd_initialize(callback);
150         if (0 != ret) {
151                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to initialize");
152                 ecore_shutdown();
153                 return ret;
154         }
155
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");
159                         ecore_shutdown();
160                         return TTSE_ERROR_OPERATION_FAILED;
161                 }
162         }
163
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()");
167                 ttse_terminate();
168                 return TTSE_ERROR_OPERATION_FAILED;
169         }
170
171         /* Register vconfkey callback to detect engine change */
172         vconf_notify_key_changed(TTS_ENGINE_DB_DEFAULT, __engine_changed_cb, NULL);
173
174         g_is_started = true;
175
176         SLOG(LOG_DEBUG, tts_tag(), "@@@");
177
178         return TTSE_ERROR_NONE;
179 }
180
181 int ttse_terminate(void)
182 {
183         if (false == is_feature_enabled()) {
184                 return TTSE_ERROR_NOT_SUPPORTED;
185         }
186
187         if (true == g_is_terminated) {
188                 SLOG(LOG_INFO, tts_tag(), "[INFO] ttse_terminate() is already invoked.");
189                 return TTSE_ERROR_NONE;
190         }
191
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;
196         }
197
198         /* Unregister vconfkey callback */
199         vconf_ignore_key_changed(TTS_ENGINE_DB_DEFAULT, __engine_changed_cb);
200
201         g_is_terminated = true;
202         g_is_started = false;
203         return TTSE_ERROR_NONE;
204 }
205
206 int ttse_get_speed_range(int* min, int* normal, int* max)
207 {
208         if (false == is_feature_enabled()) {
209                 return TTSE_ERROR_NOT_SUPPORTED;
210         }
211
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;
215         }
216
217         *min = TTS_SPEED_MIN;
218         *normal = TTS_SPEED_NORMAL;
219         *max = TTS_SPEED_MAX;
220
221         return 0;
222 }
223
224 int ttse_get_pitch_range(int* min, int* normal, int* max)
225 {
226         if (false == is_feature_enabled()) {
227                 return TTSE_ERROR_NOT_SUPPORTED;
228         }
229
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;
233         }
234
235         *min = TTS_PITCH_MIN;
236         *normal = TTS_PITCH_NORMAL;
237         *max = TTS_PITCH_MAX;
238
239         return 0;
240 }
241
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)
243 {
244         if (false == is_feature_enabled()) {
245                 return TTSE_ERROR_NOT_SUPPORTED;
246         }
247
248         int ret;
249
250         if (NULL == data) {
251                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Input parameter is null");
252         }
253
254         ret = ttsd_send_result(event, data, data_size, audio_type, rate, user_data);
255
256         if (0 != ret) {
257                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to send result");
258         }
259
260         return ret;
261 }
262
263 int ttse_send_error(ttse_error_e error, const char* msg)
264 {
265         if (false == is_feature_enabled()) {
266                 return TTSE_ERROR_NOT_SUPPORTED;
267         }
268
269         int ret;
270
271         ret = ttsd_send_error(error, msg);
272
273         if (0 != ret) {
274                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to send error");
275         }
276
277         return ret;
278 }
279
280 int ttse_set_private_data_set_cb(ttse_private_data_set_cb callback_func)
281 {
282         if (false == is_feature_enabled()) {
283                 return TTSE_ERROR_NOT_SUPPORTED;
284         }
285
286         if (NULL == callback_func) {
287                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Invalid parameter");
288                 return TTSE_ERROR_INVALID_PARAMETER;
289         }
290
291         int ret = ttsd_set_private_data_set_cb(callback_func);
292
293         if (0 != ret) {
294                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to set private data set cb");
295         }
296
297         return ret;
298 }
299
300 int ttse_set_private_data_requested_cb(ttse_private_data_requested_cb callback_func)
301 {
302         if (false == is_feature_enabled()) {
303                 return TTSE_ERROR_NOT_SUPPORTED;
304         }
305
306         if (NULL == callback_func) {
307                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Invalid parameter");
308                 return TTSE_ERROR_INVALID_PARAMETER;
309         }
310
311         int ret = ttsd_set_private_data_requested_cb(callback_func);
312
313         if (0 != ret) {
314                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to set private data requested cb");
315         }
316
317         return ret;
318 }
319
320 int ttse_get_activated_mode(int* activated_mode)
321 {
322         if (false == is_feature_enabled()) {
323                 return TTSE_ERROR_NOT_SUPPORTED;
324         }
325
326         if (NULL == activated_mode) {
327                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Invalid parameter");
328                 return TTSE_ERROR_INVALID_PARAMETER;
329         }
330
331         if (false == g_is_started) {
332                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Service engine is not started.");
333                 return TTSE_ERROR_INVALID_STATE;
334         }
335
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));
339         }
340
341         return ret;
342 }
343
344 int ttse_set_activated_mode_changed_cb(ttse_activated_mode_changed_cb callback)
345 {
346         if (false == is_feature_enabled()) {
347                 return TTSE_ERROR_NOT_SUPPORTED;
348         }
349
350         if (NULL == callback) {
351                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Invalid parameter");
352                 return TTSE_ERROR_INVALID_PARAMETER;
353         }
354
355         if (false == g_is_started) {
356                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Service engine is not started.");
357                 return TTSE_ERROR_INVALID_STATE;
358         }
359
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));
363         }
364
365         return ret;
366 }
367
368 int ttse_set_personal_tts_id_set_cb(ttse_set_personal_tts_id_cb callback)
369 {
370         if (false == is_feature_enabled()) {
371                 return TTSE_ERROR_NOT_SUPPORTED;
372         }
373
374         if (NULL == callback) {
375                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Invalid parameter");
376                 return TTSE_ERROR_INVALID_PARAMETER;
377         }
378
379         if (false == g_is_started) {
380                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Service engine is not started.");
381                 return TTSE_ERROR_INVALID_STATE;
382         }
383
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));
387         }
388
389         return ret;
390 }