tizen beta release
[platform/core/uifw/stt.git] / server / sttd_server.c
1 /*
2 * Copyright (c) 2011 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 "sttd_main.h"
16 #include "sttd_server.h"
17
18 #include "sttd_client_data.h"
19 #include "sttd_engine_agent.h"
20 #include "sttd_config.h"
21 #include "sttd_recorder.h"
22 #include "sttd_network.h"
23 #include "sttd_dbus.h"
24
25 /*
26 * STT Server static variable
27 */
28 static bool g_is_engine;
29
30 /*
31 * STT Server Callback Functions                                                                                 `                                 *
32 */
33
34 int audio_recorder_callback(const void* data, const unsigned int length)
35 {
36         if (0 != sttd_engine_recognize_audio(data, length)) {
37                 
38                 /* send message for stop */
39                 SLOG(LOG_DEBUG, TAG_STTD, "===== Fail to set recording data ");
40                         
41                 int uid = sttd_client_get_current_recording();
42
43                 app_state_e state;
44                 if (0 != sttd_client_get_state(uid, &state)) {
45                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is not valid "); 
46                         return -1;
47                 }
48
49                 if (APP_STATE_RECORDING != state) {
50                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current state is not recording"); 
51                         return -1;
52                 }
53
54                 if (0 != sttd_send_stop_recognition_by_daemon(uid)) {
55                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail "); 
56                 } else {
57                         SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] <<<< stop message : uid(%d)", uid); 
58                 }
59
60                 SLOG(LOG_DEBUG, TAG_STTD, "=====");
61                 SLOG(LOG_DEBUG, TAG_STTD, "  ");
62                 
63                 return -1;
64         }
65         
66         
67
68         return 0;
69 }
70
71 void sttd_server_recognition_result_callback(sttp_result_event_e event, const char* type, 
72                                         const char** data, int data_count, const char* msg, void *user_data)
73 {
74         SLOG(LOG_DEBUG, TAG_STTD, "===== Recognition Result Callback");
75
76         /* check uid */
77         int *uid = (int*)user_data;
78
79         SLOG(LOG_DEBUG, TAG_STTD, "[Server] uid (%d), event(%d)", uid, event); 
80
81         app_state_e state;
82         if (0 != sttd_client_get_state(*uid, &state)) {
83                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid "); 
84                 SLOG(LOG_DEBUG, TAG_STTD, "=====");
85                 SLOG(LOG_DEBUG, TAG_STTD, "  ");
86                 return;
87         }
88
89         /* send result to client */
90         if (STTP_RESULT_EVENT_SUCCESS == event && 0 < data_count && NULL != data) {
91
92                 if (APP_STATE_PROCESSING == state ) {
93                         SLOG(LOG_DEBUG, TAG_STTD, "[Server] the size of result from engine is '%d' %s", data_count); 
94
95                         if (0 != sttdc_send_result(*uid, type, data, data_count, msg)) {
96                                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result");        
97                                 int reason = (int)STTD_ERROR_OPERATION_FAILED;
98
99                                 if (0 != sttdc_send_error_signal(*uid, reason, "Fail to send recognition result")) {
100                                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info . Remove client data"); 
101
102                                         /* clean client data */
103                                         sttd_client_delete(*uid);
104                                 }
105                         }
106                 } else {
107                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current state is NOT thinking"); 
108
109                         int reason = (int)STTD_ERROR_INVALID_STATE;             
110                         if (0 != sttdc_send_error_signal(*uid, reason, "Client state is NOT thinking. Client don't receive recognition result in current state.")) {
111                                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info. Remove client data "); 
112
113                                 /* clean client data */
114                                 sttd_client_delete(*uid);               
115                         }
116                 }
117         } else if (STTP_RESULT_EVENT_NO_RESULT == event) {
118
119                 if (APP_STATE_PROCESSING == state ) {
120                         if (0 != sttdc_send_result(*uid, NULL, NULL, 0, NULL)) {
121                                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send result "); 
122
123                                 /* send error msg */
124                                 int reason = (int)STTD_ERROR_INVALID_STATE;     
125                                 if (0 != sttdc_send_error_signal(*uid, reason, "[ERROR] Fail to send recognition result")) {
126                                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info "); 
127                                         sttd_client_delete(*uid);
128                                 }
129                         }
130                 } else {
131                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current state is NOT thinking "); 
132
133                         int reason = (int)STTD_ERROR_INVALID_STATE;             
134
135                         if (0 != sttdc_send_error_signal(*uid, reason, "Client state is NOT thinking. Client don't receive recognition result in current state.")) {
136                                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info. Remove client data"); 
137
138                                 /* clean client data */
139                                 sttd_client_delete(*uid);               
140                         }
141                 }
142         } else if (STTP_RESULT_EVENT_ERROR == event) {
143                 int reason = (int)STTD_ERROR_OPERATION_FAILED;
144
145                 if (0 != sttdc_send_error_signal(*uid, reason, "STT Engine ERROR : Recognition fail")) {
146                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send error info"); 
147                         sttd_client_delete(*uid);
148                 }       
149         } else {
150                 /* nothing */
151         }
152
153         /* change state of uid */
154         sttd_client_set_state(*uid, APP_STATE_READY);
155
156         if (NULL != user_data)  
157                 free(user_data);
158
159         SLOG(LOG_DEBUG, TAG_STTD, "=====");
160         SLOG(LOG_DEBUG, TAG_STTD, "  ");
161
162         return;
163 }
164
165 void sttd_server_partial_result_callback(sttp_result_event_e event, const char* data, void *user_data)
166 {
167         SLOG(LOG_DEBUG, TAG_STTD, "===== Partial Result Callback");
168
169         /* check uid */
170         int *uid = (int*)user_data;
171
172         SLOG(LOG_DEBUG, TAG_STTD, "[Server] uid (%d), event(%d)", uid, event); 
173
174         app_state_e state;
175         if (0 != sttd_client_get_state(*uid, &state)) {
176                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid "); 
177                 SLOG(LOG_DEBUG, TAG_STTD, "=====");
178                 SLOG(LOG_DEBUG, TAG_STTD, "  ");
179                 return;
180         }
181
182         /* send result to client */
183         if (STTP_RESULT_EVENT_SUCCESS == event && NULL != data) {
184                 if (0 != sttdc_send_partial_result(*uid, data)) {
185                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to send partial result");        
186                 }
187         } 
188
189         SLOG(LOG_DEBUG, TAG_STTD, "=====");
190         SLOG(LOG_DEBUG, TAG_STTD, "  ");
191 }
192
193 void sttd_server_silence_dectection_callback(void *user_param)
194 {
195         SLOG(LOG_DEBUG, TAG_STTD, "===== Silence Detection Callback");
196
197         int uid = sttd_client_get_current_recording();
198
199         app_state_e state;
200         if (0 != sttd_client_get_state(uid, &state)) {
201                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is not valid "); 
202                 return;
203         }
204
205         if (APP_STATE_RECORDING != state) {
206                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current state is not recording"); 
207                 return;
208         }
209
210         if (0 != sttd_send_stop_recognition_by_daemon(uid)) {
211                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail "); 
212         } else {
213                 SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] <<<< stop message : uid(%d)", uid); 
214         }
215
216         SLOG(LOG_DEBUG, TAG_STTD, "=====");
217         SLOG(LOG_DEBUG, TAG_STTD, "  ");
218
219         return;
220 }
221
222 /*
223 * Daemon initialize
224 */
225
226 int sttd_initialize()
227 {
228         int ret = 0;
229
230         /* recoder init */
231         ret = sttd_recorder_init();
232         if (0 != ret) {
233                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to initialize recorder : result(%d)", ret); 
234                 return ret;
235         }
236
237         /* Engine Agent initialize */
238         ret = sttd_engine_agent_init(sttd_server_recognition_result_callback, sttd_server_partial_result_callback, 
239                                 sttd_server_silence_dectection_callback);
240         if (0 != ret) {
241                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
242                 return ret;
243         }
244         
245         if (0 != sttd_engine_agent_initialize_current_engine()) {
246                 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] There is No STT-Engine !!!!!"); 
247                 g_is_engine = false;
248         } else {
249                 g_is_engine = true;
250         }
251
252         SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] initialize"); 
253
254         return 0;
255 }
256
257 /*
258 * STT Server Functions for Client
259 */
260
261 int sttd_server_initialize(int pid, int uid)
262 {
263         if (false == g_is_engine) {
264                 if (0 != sttd_engine_agent_initialize_current_engine()) {
265                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] No Engine"); 
266                         g_is_engine = false;
267                         return STTD_ERROR_ENGINE_NOT_FOUND;
268                 } else {
269                         g_is_engine = true;
270                 }
271         }
272
273         /* check if uid is valid */
274         app_state_e state;
275         if (0 == sttd_client_get_state(uid, &state)) {
276                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid has already been registered"); 
277                 return STTD_ERROR_INVALID_PARAMETER;
278         }
279         
280         /* load if engine is unloaded */
281         if (0 == sttd_client_get_ref_count()) {
282                 if (0 != sttd_engine_agent_load_current_engine()) {
283                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to load current engine"); 
284                         return STTD_ERROR_OPERATION_FAILED;
285                 }
286         }
287
288         /* initialize recorder using audio format from engine */
289         sttp_audio_type_e atype;
290         int rate;
291         int channels;
292
293         if (0 != sttd_engine_get_audio_format(&atype, &rate, &channels)) {
294                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get audio format of engine."); 
295                 return STTD_ERROR_OPERATION_FAILED;
296         }
297
298         sttd_recorder_channel   sttchannel;
299         sttd_recorder_audio_type        sttatype;
300
301         switch (atype) {
302         case STTP_AUDIO_TYPE_PCM_S16_LE:        sttatype = STTD_RECORDER_PCM_S16;       break;
303         case STTP_AUDIO_TYPE_PCM_U8:            sttatype = STTD_RECORDER_PCM_U8;        break;
304         case STTP_AUDIO_TYPE_AMR:               sttatype = STTD_RECORDER_AMR;           break;
305         default:        
306                 /* engine error */
307                 sttd_engine_agent_unload_current_engine();
308                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Invalid Audio Type"); 
309                 return STTD_ERROR_OPERATION_FAILED;
310                 break;
311         }
312
313         switch (sttchannel) {
314         case 1:         sttchannel = STTD_RECORDER_CHANNEL_MONO;        break;
315         case 2:         sttchannel = STTD_RECORDER_CHANNEL_STEREO;      break;
316         default:        sttchannel = STTD_RECORDER_CHANNEL_MONO;        break;
317         }
318
319         if (0 != sttd_recorder_set(sttatype, sttchannel, rate, 60, audio_recorder_callback)) {
320                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set recorder"); 
321                 return STTD_ERROR_OPERATION_FAILED;
322         }
323         
324         /* Add client information to client manager */
325         if (0 != sttd_client_add(pid, uid)) {
326                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to add client info"); 
327                 return STTD_ERROR_OPERATION_FAILED;
328         }
329
330         SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] audio type(%d), channel(%d)", (int)atype, (int)sttchannel); 
331
332         return STTD_ERROR_NONE;
333 }
334
335 int sttd_server_finalize(const int uid)
336 {
337         /* check if uid is valid */
338         app_state_e state;
339         if (0 != sttd_client_get_state(uid, &state)) {
340                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid "); 
341                 return STTD_ERROR_INVALID_PARAMETER;
342         }
343
344         /* release recorder */
345         app_state_e appstate;
346         sttd_client_get_state(uid, &appstate);
347
348         if (APP_STATE_RECORDING == appstate || APP_STATE_PROCESSING == appstate) {
349                 sttd_recorder_cancel();
350                 sttd_engine_recognize_cancel();
351         }
352         
353         /* Remove client information */
354         if (0 != sttd_client_delete(uid)) {
355                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to delete client"); 
356         }
357
358         /* unload engine, if ref count of client is 0 */
359         if (0 == sttd_client_get_ref_count()) {
360                 if (0 != sttd_engine_agent_unload_current_engine()) {
361                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unload current engine"); 
362                 } else {
363                         SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] unload current engine"); 
364                 }
365         }
366         
367         return STTD_ERROR_NONE;
368 }
369
370 int sttd_server_get_supported_languages(const int uid, GList** lang_list)
371 {
372         /* check if uid is valid */
373         app_state_e state;
374         if (0 != sttd_client_get_state(uid, &state)) {
375                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid "); 
376                 return STTD_ERROR_INVALID_PARAMETER;
377         }
378
379         /* get language list from engine */
380         int ret = sttd_engine_supported_langs(lang_list);
381         if (0 != ret) {
382                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get supported languages"); 
383                 return STTD_ERROR_OPERATION_FAILED;
384         }
385
386         SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] sttd_server_get_supported_languages"); 
387
388         return STTD_ERROR_NONE;
389 }
390
391 int sttd_server_get_current_langauage(const int uid, char** current_lang)
392 {
393         /* check if uid is valid */
394         app_state_e state;
395         if (0 != sttd_client_get_state(uid, &state)) {
396                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid "); 
397                 return STTD_ERROR_INVALID_PARAMETER;
398         }
399
400         if (NULL == current_lang) {
401                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL"); 
402                 return STTD_ERROR_INVALID_PARAMETER;
403         }
404
405         /*get current language from engine */
406         int ret = sttd_engine_get_default_lang(current_lang);
407         if (0 != ret) { 
408                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get default language :result(%d)", ret); 
409                 return STTD_ERROR_OPERATION_FAILED;
410         }
411
412         SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] sttd_engine_get_default_lang"); 
413
414         return STTD_ERROR_NONE;
415 }
416
417 int sttd_server_is_partial_result_supported(int uid, int* partial_result)
418 {
419         /* check if uid is valid */
420         app_state_e state;
421         if (0 != sttd_client_get_state(uid, &state)) {
422                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid "); 
423                 return STTD_ERROR_INVALID_PARAMETER;
424         }
425
426         if (NULL == partial_result) {
427                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL"); 
428                 return STTD_ERROR_INVALID_PARAMETER;
429         }
430
431         bool temp;
432         int ret = sttd_engine_is_partial_result_supported(&temp);
433         if (0 != ret) { 
434                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get partial result supported : result(%d)", ret); 
435                 return STTD_ERROR_OPERATION_FAILED;
436         }
437
438         SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] Partial result supporting is %s", temp ? "true" : "false"); 
439
440         return STTD_ERROR_NONE;
441 }
442
443 int sttd_server_get_audio_volume( const int uid, float* current_volume)
444 {
445         /* check if uid is valid */
446         app_state_e state;
447         if (0 != sttd_client_get_state(uid, &state)) {
448                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid "); 
449                 return STTD_ERROR_INVALID_PARAMETER;
450         }
451
452         /* check uid state */
453         if (APP_STATE_RECORDING != state) {
454                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current state is not recording"); 
455                 return STTD_ERROR_INVALID_STATE;
456         }
457
458         if (NULL == current_volume) {
459                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL"); 
460                 return STTD_ERROR_INVALID_PARAMETER;
461         }
462
463         /* get audio volume from recorder */
464         int ret = sttd_recorder_get_volume(current_volume);
465         if (0 != ret) {
466                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get volume : result(%d)", ret); 
467                 return STTD_ERROR_OPERATION_FAILED;
468         }
469
470         return STTD_ERROR_NONE;
471 }
472
473 int sttd_server_start(const int uid, const char* lang, const char* recognition_type, 
474                       int profanity, int punctuation, int silence)
475 {
476         /* check if uid is valid */
477         app_state_e state;
478         if (0 != sttd_client_get_state(uid, &state)) {
479                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid "); 
480                 return STTD_ERROR_INVALID_PARAMETER;
481         }
482
483         /* check uid state */
484         if (APP_STATE_READY != state) {
485                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] sttd_server_start : current state is not ready"); 
486                 return STTD_ERROR_INVALID_STATE;
487         }
488
489         if (0 < sttd_client_get_current_recording()) {
490                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current STT Engine is busy because of recording");
491                 return STTD_ERROR_RECORDER_BUSY;
492         }
493
494         if (0 < sttd_client_get_current_thinking()) {
495                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current STT Engine is busy because of thinking");
496                 return STTD_ERROR_RECORDER_BUSY;
497         }
498
499         /* check if engine use network */
500         if (true == sttd_engine_agent_need_network()) {
501                 if (false == sttd_network_is_connected()) {
502                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Disconnect network. Current engine needs to network connection.");
503                         return STTD_ERROR_OUT_OF_NETWORK;
504                 }
505         }
506
507         /* engine start recognition */
508         int* user_data;
509         user_data = (int*)malloc( sizeof(int) * 1);
510         
511         /* free on result callback */
512         *user_data = uid;
513
514         SLOG(LOG_DEBUG, TAG_STTD, "[Server] start : uid(%d), lang(%s), recog_type(%s)", *user_data, lang, recognition_type ); 
515
516         int ret = sttd_engine_recognize_start((char*)lang, recognition_type, profanity, punctuation, silence, (void*)user_data);
517         if (0 != ret) {
518                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recognition : result(%d)", ret); 
519                 return STTD_ERROR_OPERATION_FAILED;
520         }
521
522         /* recorder start */
523         ret = sttd_recorder_start();
524         if (0 != ret) {
525                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to start recorder : result(%d)", ret); 
526                 sttd_engine_recognize_cancel();
527                 return STTD_ERROR_OPERATION_FAILED;
528         }
529
530         SLOG(LOG_DEBUG, TAG_STTD, "[Server] start recording"); 
531
532         /* change uid state */
533         sttd_client_set_state(uid, APP_STATE_RECORDING);
534
535         return STTD_ERROR_NONE;
536 }
537
538 int sttd_server_stop(const int uid)
539 {
540         /* check if uid is valid */
541         app_state_e state;
542         if (0 != sttd_client_get_state(uid, &state)) {
543                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid "); 
544                 return STTD_ERROR_INVALID_PARAMETER;
545         }
546
547         /* check uid state */
548         if (APP_STATE_RECORDING != state) {
549                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Current state is not recording"); 
550                 return STTD_ERROR_INVALID_STATE;
551         }
552
553         /* stop recorder */
554         sttd_recorder_stop();
555
556         /* stop engine recognition */
557         int ret = sttd_engine_recognize_stop();
558         if (0 != ret) {
559                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to stop : result(%d)", ret); 
560                 sttd_client_set_state(uid, APP_STATE_READY);            
561         
562                 return STTD_ERROR_OPERATION_FAILED;
563         }
564
565         /* change uid state */
566         sttd_client_set_state(uid, APP_STATE_PROCESSING);
567
568         return STTD_ERROR_NONE;
569 }
570
571 int sttd_server_cancel(const int uid)
572 {
573         /* check if uid is valid */
574         app_state_e state;
575         if (0 != sttd_client_get_state(uid, &state)) {
576                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid "); 
577                 return STTD_ERROR_INVALID_PARAMETER;
578         }
579
580         /* check uid state */ 
581         if (APP_STATE_READY == state) {
582                 SLOG(LOG_WARN, TAG_STTD, "[Server WARNING] Current state is ready"); 
583                 return STTD_ERROR_INVALID_STATE;
584         }
585
586         /* stop recorder */
587         if (APP_STATE_RECORDING == state) 
588                 sttd_recorder_cancel();
589
590         /* cancel engine recognition */
591         int ret = sttd_engine_recognize_cancel();
592         if (0 != ret) {
593                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to cancel : result(%d)", ret); 
594                 return STTD_ERROR_OPERATION_FAILED;
595         }
596
597         /* change uid state */
598         sttd_client_set_state(uid, APP_STATE_READY);
599
600         return STTD_ERROR_NONE;
601 }
602
603
604 /******************************************************************************************
605 * STT Server Functions for setting
606 *******************************************************************************************/
607
608 int sttd_server_setting_initialize(int uid)
609 {
610         if (false == g_is_engine) {
611                 if (0 != sttd_engine_agent_initialize_current_engine()) {
612                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] No Engine"); 
613                         g_is_engine = false;
614                         return STTD_ERROR_ENGINE_NOT_FOUND;
615                 } else {
616                         g_is_engine = true;
617                 }
618         }
619
620         /* check if uid is valid */
621         app_state_e state;
622         if (0 == sttd_client_get_state(uid, &state)) {
623                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid has already been registered"); 
624                 return STTD_ERROR_INVALID_PARAMETER;
625         }
626
627         /* load if engine is unloaded */
628         if (0 == sttd_client_get_ref_count()) {
629                 if (0 != sttd_engine_agent_load_current_engine()) {
630                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to load current engine"); 
631                         return STTD_ERROR_OPERATION_FAILED;
632                 }
633         }
634
635         /* Add client information to client manager (For internal use) */
636         if (0 != sttd_client_add(uid, uid)) {
637                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to add client info"); 
638                 return STTD_ERROR_OPERATION_FAILED;
639         }
640
641         return STTD_ERROR_NONE;
642 }
643
644 int sttd_server_setting_finalize(int uid)
645 {
646         /* check if uid is valid */
647         app_state_e state;
648         if (0 != sttd_client_get_state(uid, &state)) {
649                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid "); 
650                 return STTD_ERROR_INVALID_PARAMETER;
651         }
652
653         /* Remove client information */
654         if (0 != sttd_client_delete(uid)) {
655                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to delete setting client"); 
656         }
657
658         /* unload engine, if ref count of client is 0 */
659         if (0 == sttd_client_get_ref_count()) {
660                 if (0 != sttd_engine_agent_unload_current_engine()) {
661                         SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to unload current engine"); 
662                 } else {
663                         SLOG(LOG_DEBUG, TAG_STTD, "[Server SUCCESS] unload current engine"); 
664                 }
665         }
666
667         return STTD_ERROR_NONE;
668 }
669
670 int sttd_server_setting_get_engine_list(int uid, GList** engine_list)
671 {
672         /* check if uid is valid */
673         app_state_e state;
674         if (0 != sttd_client_get_state(uid, &state)) {
675                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid "); 
676                 return STTD_ERROR_INVALID_PARAMETER;
677         }
678
679         if (NULL == engine_list) {
680                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL"); 
681                 return STTD_ERROR_INVALID_PARAMETER;
682         }
683
684         int ret = sttd_engine_setting_get_engine_list(engine_list); 
685         if (0 != ret) {
686                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine list : result(%d)", ret); 
687                 return STTD_ERROR_OPERATION_FAILED;
688         }
689
690         return STTD_ERROR_NONE;
691 }
692
693 int sttd_server_setting_get_engine(int uid, char** engine_id)
694 {
695         app_state_e state;
696         if (0 != sttd_client_get_state(uid, &state)) {
697                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid "); 
698                 return STTD_ERROR_INVALID_PARAMETER;
699         }
700
701         if (NULL == engine_id) {
702                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL"); 
703                 return STTD_ERROR_INVALID_PARAMETER;
704         }
705
706         /* get engine */
707         int ret = sttd_engine_setting_get_engine(engine_id); 
708         if (0 != ret) {
709                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine : result(%d)", ret); 
710                 return STTD_ERROR_OPERATION_FAILED;
711         }
712
713         return STTD_ERROR_NONE;
714 }
715
716 int sttd_server_setting_set_engine(const int uid, const char* engine_id)
717 {
718         /* check if uid is valid */
719         app_state_e state;
720         if (0 != sttd_client_get_state(uid, &state)) {
721                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid "); 
722                 return STTD_ERROR_INVALID_PARAMETER;
723         }
724
725         if (NULL == engine_id) {
726                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL"); 
727                 return STTD_ERROR_INVALID_PARAMETER;
728         }
729
730         /* set engine */
731         int ret = sttd_engine_setting_set_engine(engine_id); 
732         if (0 != ret) {
733                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set engine : result(%d)", ret); 
734                 return STTD_ERROR_OPERATION_FAILED;
735         }
736
737         return STTD_ERROR_NONE;
738 }
739
740 int sttd_server_setting_get_lang_list(int uid, char** engine_id, GList** lang_list)
741 {
742         /* check if uid is valid */
743         app_state_e state;
744         if (0 != sttd_client_get_state(uid, &state)) {
745                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid "); 
746                 return STTD_ERROR_INVALID_PARAMETER;
747         }
748         
749         if (NULL == lang_list) {
750                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] language is NULL"); 
751                 return STTD_ERROR_INVALID_PARAMETER;
752         }
753         
754         int ret = sttd_engine_setting_get_lang_list(engine_id, lang_list); 
755         if (0 != ret) {
756                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get language list : result(%d)", ret); 
757                 return STTD_ERROR_OPERATION_FAILED;
758         }
759
760         return STTD_ERROR_NONE;
761 }
762
763 int sttd_server_setting_get_default_language(int uid, char** language)
764 {
765         /* check if uid is valid */
766         app_state_e state;
767         if (0 != sttd_client_get_state(uid, &state)) {
768                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid "); 
769                 return STTD_ERROR_INVALID_PARAMETER;
770         }
771
772         if (NULL == language) {
773                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] language is NULL"); 
774                 return STTD_ERROR_INVALID_PARAMETER;
775         }
776
777         int ret = sttd_engine_setting_get_default_lang(language); 
778         if (0 != ret) {
779                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get default language : result(%d)", ret); 
780                 return STTD_ERROR_OPERATION_FAILED;
781         }       
782
783         return STTD_ERROR_NONE;
784 }
785
786 int sttd_server_setting_set_default_language(int uid, const char* language)
787 {
788         /* check if uid is valid */
789         app_state_e state;
790         if (0 != sttd_client_get_state(uid, &state)) {
791                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid "); 
792                 return STTD_ERROR_INVALID_PARAMETER;
793         }
794
795         if (NULL == language) {
796                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] language is NULL"); 
797                 return STTD_ERROR_INVALID_PARAMETER;
798         }
799
800         int ret = sttd_engine_setting_set_default_lang((char*)language); 
801         if (0 != ret) {
802                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set default lang : result(%d)", ret); 
803                 return STTD_ERROR_OPERATION_FAILED;
804         }       
805
806         return STTD_ERROR_NONE;
807 }
808
809 int sttd_server_setting_get_profanity_filter(int uid, bool* value)
810 {
811         /* check if uid is valid */
812         app_state_e state;
813         if (0 != sttd_client_get_state(uid, &state)) {
814                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid "); 
815                 return STTD_ERROR_INVALID_PARAMETER;
816         }
817
818         if (NULL == value) {
819                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL"); 
820                 return STTD_ERROR_INVALID_PARAMETER;
821         }
822
823         int ret = 0;
824         ret = sttd_engine_setting_get_profanity_filter(value);
825         if (0 != ret) {
826                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get profanity filter : result(%d)", ret); 
827                 return STTD_ERROR_OPERATION_FAILED;
828         }       
829
830         return STTD_ERROR_NONE;
831 }
832
833 int sttd_server_setting_set_profanity_filter(int uid, bool value)
834 {
835         /* check if uid is valid */
836         app_state_e state;
837         if (0 != sttd_client_get_state(uid, &state)) {
838                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid "); 
839                 return STTD_ERROR_INVALID_PARAMETER;
840         }
841
842         int ret = 0;
843         ret = sttd_engine_setting_set_profanity_filter(value);
844         if (0 != ret) {
845                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set profanity filter: result(%d)", ret); 
846                 return STTD_ERROR_OPERATION_FAILED;
847         }       
848
849         return STTD_ERROR_NONE;
850 }
851
852 int sttd_server_setting_get_punctuation_override(int uid, bool* value)
853 {
854         /* check if uid is valid */
855         app_state_e state;
856         if (0 != sttd_client_get_state(uid, &state)) {
857                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid "); 
858                 return STTD_ERROR_INVALID_PARAMETER;
859         }
860
861         if (NULL == value) {
862                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL"); 
863                 return STTD_ERROR_INVALID_PARAMETER;
864         }
865
866         int ret = 0;
867         ret = sttd_engine_setting_get_punctuation_override(value);
868         if (0 != ret) {
869                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get punctuation override : result(%d)", ret); 
870                 return STTD_ERROR_OPERATION_FAILED;
871         }       
872
873         return STTD_ERROR_NONE;
874 }
875
876 int sttd_server_setting_set_punctuation_override(int uid, bool value)
877 {
878         /* check if uid is valid */
879         app_state_e state;
880         if (0 != sttd_client_get_state(uid, &state)) {
881                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid "); 
882                 return STTD_ERROR_INVALID_PARAMETER;
883         }
884
885         int ret = 0;
886         ret = sttd_engine_setting_set_punctuation_override(value);
887         if (0 != ret) {
888                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set punctuation override : result(%d)", ret); 
889                 return STTD_ERROR_OPERATION_FAILED;
890         }       
891
892         return STTD_ERROR_NONE;
893 }
894
895 int sttd_server_setting_get_silence_detection(int uid, bool* value)
896 {
897         /* check if uid is valid */
898         app_state_e state;
899         if (0 != sttd_client_get_state(uid, &state)) {
900                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid "); 
901                 return STTD_ERROR_INVALID_PARAMETER;
902         }
903
904         if (NULL == value) {
905                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Input parameter is NULL"); 
906                 return STTD_ERROR_INVALID_PARAMETER;
907         }
908
909         int ret = 0;
910         ret = sttd_engine_setting_get_silence_detection(value);
911         if (0 != ret) {
912                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get silence detection : result(%d)", ret); 
913                 return STTD_ERROR_OPERATION_FAILED;
914         }       
915
916         return STTD_ERROR_NONE;
917 }
918
919 int sttd_server_setting_set_silence_detection(int uid, bool value)
920 {
921         /* check if uid is valid */
922         app_state_e state;
923         if (0 != sttd_client_get_state(uid, &state)) {
924                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid "); 
925                 return STTD_ERROR_INVALID_PARAMETER;
926         }
927
928         int ret = 0;
929         ret = sttd_engine_setting_set_silence_detection(value);
930         if (0 != ret) {
931                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to Result(%d)", ret); 
932                 return STTD_ERROR_OPERATION_FAILED;
933         }       
934
935         return STTD_ERROR_NONE;
936 }
937
938 int sttd_server_setting_get_engine_setting(int uid, char** engine_id, GList** lang_list)
939 {       
940         /* check if uid is valid */
941         app_state_e state;
942         if (0 != sttd_client_get_state(uid, &state)) {
943                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid "); 
944                 return STTD_ERROR_INVALID_PARAMETER;
945         }
946
947         if (0 != sttd_engine_setting_get_engine_setting_info(engine_id, lang_list)) {
948                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to get engine setting info"); 
949                 return STTD_ERROR_OPERATION_FAILED;
950         }
951
952         return STTD_ERROR_NONE;
953 }
954
955 int sttd_server_setting_set_engine_setting(int uid, const char* key, const char* value)
956 {       
957         /* check if uid is valid */
958         app_state_e state;
959         if (0 != sttd_client_get_state(uid, &state)) {
960                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] uid is NOT valid "); 
961                 return STTD_ERROR_INVALID_PARAMETER;
962         }
963
964         if (0 != sttd_engine_setting_set_engine_setting(key, value)) {
965                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to set engine setting info"); 
966                 return STTD_ERROR_OPERATION_FAILED;
967         }
968
969         return STTD_ERROR_NONE;
970 }
971
972