Implement behavior of new APIs
[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
28 #include "ttse.h"
29
30 static bool g_is_terminated = false;
31
32 const char* tts_tag()
33 {
34         return "ttsd";
35 }
36
37 static bool __is_default_engine()
38 {
39         char* engine = NULL;
40         engine = vconf_get_str(VCONFKEY_TTS_ENGINE_DEFAULT);
41         if (NULL == engine) {
42                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get sting for vc engine");
43                 return FALSE;
44         }
45
46         char appid[1024] = {'\0', };
47         if (0 != aul_app_get_appid_bypid(getpid(), appid, sizeof(appid) - 1)) {
48                 SLOG(LOG_ERROR, tts_tag(), "[Server ERROR] Fail to get callee appid by pid");
49         }
50
51         SLOG(LOG_DEBUG, tts_tag(), "[Server] TTS Default Engine(%s), appId(%s)", engine, appid);
52         if (0 == strncmp(engine, appid, strlen(engine))) {
53                 free(engine);
54                 return TRUE;
55         }
56         free(engine);
57         return FALSE;
58 }
59
60 static void __engine_changed_cb(keynode_t* key, void* data)
61 {
62         SLOG(LOG_INFO, tts_tag(), "[INFO] TTS engine vconfkey is changed");
63
64         /* If a new TTS engine is different from the current engine, call ttse_terminate() */
65         if (FALSE == __is_default_engine()) {
66                 SLOG(LOG_WARN, tts_tag(), "[WARNING] TTS engine is changed. Please call ttse_terminate()");
67                 ttse_terminate();
68                 ecore_main_loop_quit();
69         } else {
70                 SLOG(LOG_INFO, tts_tag(), "[INFO] A new TTS engine is same as the current engine.");
71         }
72
73         return;
74 }
75
76 static bool __is_test_app()
77 {
78         char* appid = NULL;
79         int ret = app_manager_get_app_id(getpid(), &appid);
80         if (APP_MANAGER_ERROR_NONE != ret || NULL == appid) {
81                 return false;
82         }
83         SLOG(LOG_INFO, tts_tag(), "[INFO] app id (%s)", appid);
84
85         bool is_test_app = false;
86         if (0 == strncmp(appid, "org.tizen.tts-native-itc", 32) || 0 == strncmp(appid, "org.tizen.tts-native-utc", 32)) {
87                 SLOG(LOG_INFO, tts_tag(), "[INFO] Test mode is on");
88                 is_test_app = true;
89         }
90
91         free(appid);
92         return is_test_app;
93 }
94
95 int ttse_main(int argc, char** argv, ttse_request_callback_s *callback)
96 {
97         g_is_terminated = false;
98
99         if (!ecore_init()) {
100                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to initialize Ecore");
101                 return TTSE_ERROR_OPERATION_FAILED;
102         }
103
104         int ret = ttsd_initialize(callback);
105         if (0 != ret) {
106                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to initialize");
107                 ecore_shutdown();
108                 return ret;
109         }
110
111         if (TRUE == __is_default_engine()) {
112                 if (0 != ttsd_ipc_open_connection()) {
113                         SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to open ipc connection");
114                         ecore_shutdown();
115                         return TTSE_ERROR_OPERATION_FAILED;
116                 }
117         }
118
119         /* If a new TTS engine is different from the current engine, call ttse_terminate() */
120         if (FALSE == __is_default_engine() && !__is_test_app()) {
121                 SLOG(LOG_WARN, tts_tag(), "[WARNING] Before beginning main loop of TTS engine, default TTS engine is changed. Please call ttse_terminate()");
122                 ttse_terminate();
123                 return TTSE_ERROR_OPERATION_FAILED;
124         }
125
126         /* Register vconfkey callback to detect engine change */
127         vconf_notify_key_changed(TTS_ENGINE_DB_DEFAULT, __engine_changed_cb, NULL);
128
129         SLOG(LOG_DEBUG, tts_tag(), "@@@");
130
131         return TTSE_ERROR_NONE;
132 }
133
134 int ttse_terminate(void)
135 {
136         if (true == g_is_terminated) {
137                 SLOG(LOG_INFO, tts_tag(), "[INFO] ttse_terminate() is already invoked.");
138                 return TTSE_ERROR_NONE;
139         }
140
141         int ret = ttsd_terminate();
142         if (TTSD_ERROR_NONE != ret) {
143                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to ttsd_terminate(). ret(%d)", ret);
144                 return TTSE_ERROR_OPERATION_FAILED;
145         }
146
147         /* Unregister vconfkey callback */
148         vconf_ignore_key_changed(TTS_ENGINE_DB_DEFAULT, __engine_changed_cb);
149
150         g_is_terminated = true;
151         return TTSE_ERROR_NONE;
152 }
153
154 int ttse_get_speed_range(int* min, int* normal, int* max)
155 {
156         if (NULL == min || NULL == normal || NULL == max) {
157                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Input parameter is null");
158                 return TTSE_ERROR_INVALID_PARAMETER;
159         }
160
161         *min = TTS_SPEED_MIN;
162         *normal = TTS_SPEED_NORMAL;
163         *max = TTS_SPEED_MAX;
164
165         return 0;
166 }
167
168 int ttse_get_pitch_range(int* min, int* normal, int* max)
169 {
170         if (NULL == min || NULL == normal || NULL == max) {
171                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Input parameter is null");
172                 return TTSE_ERROR_INVALID_PARAMETER;
173         }
174
175         *min = TTS_PITCH_MIN;
176         *normal = TTS_PITCH_NORMAL;
177         *max = TTS_PITCH_MAX;
178
179         return 0;
180 }
181
182 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)
183 {
184         int ret;
185
186         if (NULL == data) {
187                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Input parameter is null");
188         }
189
190         ret = ttsd_send_result(event, data, data_size, audio_type, rate, user_data);
191
192         if (0 != ret) {
193                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to send result");
194         }
195
196         return ret;
197 }
198
199 int ttse_send_error(ttse_error_e error, const char* msg)
200 {
201         int ret;
202
203         ret = ttsd_send_error(error, msg);
204
205         if (0 != ret) {
206                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to send error");
207         }
208
209         return ret;
210 }
211
212 int ttse_set_private_data_set_cb(ttse_private_data_set_cb callback_func)
213 {
214         if (NULL == callback_func) {
215                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Invalid parameter");
216                 return TTSE_ERROR_INVALID_PARAMETER;
217         }
218
219         int ret = ttsd_set_private_data_set_cb(callback_func);
220
221         if (0 != ret) {
222                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to set private data set cb");
223         }
224
225         return ret;
226 }
227
228 int ttse_set_private_data_requested_cb(ttse_private_data_requested_cb callback_func)
229 {
230         if (NULL == callback_func) {
231                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Invalid parameter");
232                 return TTSE_ERROR_INVALID_PARAMETER;
233         }
234
235         int ret = ttsd_set_private_data_requested_cb(callback_func);
236
237         if (0 != ret) {
238                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to set private data requested cb");
239         }
240
241         return ret;
242 }
243
244 int ttse_get_activated_mode(int* activated_mode)
245 {
246         if (NULL == activated_mode) {
247                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Invalid parameter");
248                 return TTSE_ERROR_INVALID_PARAMETER;
249         }
250
251         if (g_is_terminated) {
252                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Service engine is terminated.");
253                 return TTSE_ERROR_INVALID_STATE;
254         }
255
256         int ret = ttsd_get_activated_mode(activated_mode);
257         if (TTSD_ERROR_NONE != ret) {
258                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to get activated mode. ret(%d/%s)", ret, get_error_message(ret));
259         }
260
261         return ret;
262 }
263
264 int ttse_set_activated_mode_changed_cb(ttse_activated_mode_changed_cb callback)
265 {
266         if (NULL == callback) {
267                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Invalid parameter");
268                 return TTSE_ERROR_INVALID_PARAMETER;
269         }
270
271         if (g_is_terminated) {
272                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Service engine is terminated.");
273                 return TTSE_ERROR_INVALID_STATE;
274         }
275
276         int ret = ttsd_set_activated_mode_changed_cb(callback);
277         if (TTSD_ERROR_NONE != ret) {
278                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to set activated mode changed cb. ret(%d/%s)", ret, get_error_message(ret));
279         }
280
281         return ret;
282 }