2 * Copyright (c) 2011-2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <sound_manager.h>
20 #include "vc_info_parser.h"
22 #include "vcd_server.h"
23 #include "vcd_client_data.h"
25 #include "vcd_engine_agent.h"
26 #include "vcd_config.h"
27 #include "vcd_recorder.h"
30 #include "voice_control_command_expand.h"
33 * VC Server static variable
35 static bool g_is_engine;
38 * VC Server Internal Functions
40 static Eina_Bool __stop_by_silence(void *data)
42 SLOG(LOG_DEBUG, TAG_VCD, "===== Silence Detected ");
44 vcd_server_mgr_stop();
46 SLOG(LOG_DEBUG, TAG_VCD, "=====");
47 SLOG(LOG_DEBUG, TAG_VCD, " ");
51 static Eina_Bool __cancel_by_interrupt(void *data)
53 SLOG(LOG_DEBUG, TAG_VCD, "===== Cancel by interrupt");
55 vcd_server_mgr_cancel();
57 SLOG(LOG_DEBUG, TAG_VCD, "=====");
58 SLOG(LOG_DEBUG, TAG_VCD, " ");
62 static Eina_Bool __restart_engine(void *data)
64 SLOG(LOG_DEBUG, TAG_VCD, "===== Restart by no result");
66 /* Restart recognition */
67 int ret = vcd_engine_recognize_start(true);
69 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to restart recognition : result(%d)", ret);
73 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
75 if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
76 vcd_config_set_service_state(VCD_STATE_RECORDING);
77 vcdc_send_service_state(VCD_STATE_RECORDING);
80 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Restart recognition");
82 SLOG(LOG_DEBUG, TAG_VCD, "=====");
83 SLOG(LOG_DEBUG, TAG_VCD, " ");
87 static int __server_recorder_callback(const void* data, const unsigned int length)
89 vcd_state_e state = vcd_config_get_service_state();
90 if (VCD_STATE_RECORDING != state) {
91 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Skip by engine processing");
95 vcp_speech_detect_e speech_detected = VCP_SPEECH_DETECT_NONE;
98 ret = vcd_engine_recognize_audio(data, length, &speech_detected);
102 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set recording data to engine(%d)", ret);
103 ecore_timer_add(0, __cancel_by_interrupt, NULL);
107 if (VCP_SPEECH_DETECT_BEGIN == speech_detected) {
108 if (-1 != vcd_client_manager_get_pid()) {
109 /* Manager client is available */
110 if (0 != vcdc_send_speech_detected(vcd_client_manager_get_pid())) {
111 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send speech detected");
114 } else if (VCP_SPEECH_DETECT_END == speech_detected) {
115 if (VCD_RECOGNITION_MODE_STOP_BY_SILENCE == vcd_client_get_recognition_mode()) {
116 /* silence detected */
117 ecore_timer_add(0, __stop_by_silence, NULL);
118 } else if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
119 /* Stop engine recognition */
120 int ret = vcd_engine_recognize_stop();
122 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
124 vcd_config_set_service_state(VCD_STATE_PROCESSING);
125 vcdc_send_service_state(VCD_STATE_PROCESSING);
127 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop engine only by silence");
128 } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == vcd_client_get_recognition_mode()) {
129 /* Stop engine recognition */
130 int ret = vcd_engine_recognize_stop();
132 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
140 void __server_recorder_interrupt_callback()
142 SLOG(LOG_DEBUG, TAG_VCD, "===== Cancel by sound interrupt");
144 ecore_timer_add(0, __cancel_by_interrupt, NULL);
146 SLOG(LOG_DEBUG, TAG_VCD, "=====");
147 SLOG(LOG_DEBUG, TAG_VCD, " ");
150 static void __config_lang_changed_cb(const char* current_lang, void* user_data)
152 SLOG(LOG_DEBUG, TAG_VCD, "===== Change language ");
154 /* Current state is recording */
155 vcd_state_e state = vcd_config_get_service_state();
156 if (VCD_STATE_RECORDING == state || VCD_STATE_PROCESSING == state) {
157 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is 'Recording'. Cancel recognition");
158 vcd_server_mgr_cancel();
162 ret = vcd_engine_set_current_language(current_lang);
164 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set language of engine : %d", ret);
167 SLOG(LOG_DEBUG, TAG_VCD, "=====");
168 SLOG(LOG_DEBUG, TAG_VCD, " ");
173 static void __config_foreground_changed_cb(int previous, int current, void* user_data)
175 SLOG(LOG_DEBUG, TAG_VCD, "===== Change foreground");
177 SLOG(LOG_DEBUG, TAG_VCD, "Foreground pid(%d)", current);
179 if (VC_NO_FOREGROUND_PID != current) {
180 /* Foreground app is changed */
181 vcd_state_e state = vcd_config_get_service_state();
182 if (VCD_STATE_RECORDING == state) {
183 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Foreground pid(%d) is changed. Cancel recognition", current);
184 ecore_timer_add(0, __cancel_by_interrupt, NULL);
188 SLOG(LOG_DEBUG, TAG_VCD, "=====");
189 SLOG(LOG_DEBUG, TAG_VCD, " ");
194 static Eina_Bool __vcd_send_selected_result(void *data)
196 GSList* pid_list = NULL;
197 if (0 != vc_info_parser_get_result_pid_list(&pid_list)) {
198 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to get pid list. No result");
200 if (0 < g_slist_length(pid_list)) {
202 vc_cmd_s* temp_cmd = NULL;
206 iter = g_slist_nth(pid_list, 0);
207 while (NULL != iter) {
208 temp_cmd = iter->data;
210 if (NULL != temp_cmd) {
213 /* send result noti */
214 ret = vcdc_send_result(temp_cmd->pid, temp_cmd->type);
216 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
217 if (VCD_ERROR_TIMED_OUT != ret) {
221 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Send result : pid(%d) type(%d)", temp_cmd->pid, temp_cmd->type);
225 if (100 == count) break;
226 /* While is retry code */
231 pid_list = g_slist_remove_link(pid_list, iter);
232 iter = g_slist_nth(pid_list, 0);
237 if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY != vcd_client_get_recognition_mode()) {
238 vcd_config_set_service_state(VCD_STATE_READY);
239 vcdc_send_service_state(VCD_STATE_READY);
245 static void __vcd_server_result_cb(vcp_result_event_e event, int* result_id, int count, const char* all_result,
246 const char* non_fixed_result, const char* msg, void *user_data)
248 if (VCD_STATE_PROCESSING != vcd_config_get_service_state()) {
249 if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY != vcd_client_get_recognition_mode()) {
250 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not 'Processing' and mode is not 'Restart continuously'");
255 vc_info_parser_unset_result(vcd_client_manager_get_exclusive());
257 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Event(%d), Text(%s) Nonfixed(%s) Msg(%s) Result count(%d)",
258 event, all_result, non_fixed_result, msg, count);
260 if (VCD_RECOGNITION_MODE_RESTART_AFTER_REJECT == vcd_client_get_recognition_mode()) {
261 if (VCP_RESULT_EVENT_REJECTED == event) {
262 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart by no or rejected result");
263 /* If no result and restart option is ON */
264 /* Send reject message */
265 bool temp = vcd_client_manager_get_exclusive();
266 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
267 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
268 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
271 ecore_timer_add(0, __restart_engine, NULL);
274 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Stop recorder due to success");
276 } else if (VCD_RECOGNITION_MODE_RESTART_CONTINUOUSLY == vcd_client_get_recognition_mode()) {
277 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Restart continuously");
278 /* Restart option is ON */
279 ecore_timer_add(0, __restart_engine, NULL);
280 if (VCP_RESULT_EVENT_REJECTED == event) {
281 bool temp = vcd_client_manager_get_exclusive();
282 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
283 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NOTIFICATION)) {
284 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
291 if (NULL == result_id) {
293 if (NULL != all_result) {
294 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Engine result is no command : %s", all_result);
295 bool temp = vcd_client_manager_get_exclusive();
296 vc_info_parser_set_result(all_result, event, msg, NULL, temp);
299 int pid = vcd_client_widget_get_foreground_pid();
301 if (NULL != all_result) {
302 /* Send result text to widget */
303 vcdc_send_result(pid, VC_COMMAND_TYPE_WIDGET);
306 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
307 /* Send to hide tooltip */
308 vcdc_send_show_tooltip(pid, false);
311 if (-1 != vcd_client_manager_get_pid()) {
312 /* Manager client is available */
313 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
314 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
318 vcd_client_manager_set_exclusive(false);
324 SLOG(LOG_DEBUG, TAG_VCD, "[Server] === Get engine result ===");
327 vc_cmd_s* temp_cmd = NULL;
328 vc_cmd_list_h vc_cmd_list = NULL;
330 if (0 != vc_cmd_list_create(&vc_cmd_list)) {
331 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to create command list");
332 vcd_client_manager_set_exclusive(false);
333 vcd_config_set_service_state(VCD_STATE_READY);
334 vcdc_send_service_state(VCD_STATE_READY);
339 for (i = 0; i < count; i++) {
340 SLOG(LOG_DEBUG, TAG_VCD, "[Server] [%d] Result ID(%d)", i, result_id[i]);
342 if (result_id[i] < 0) {
343 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Result ID(%d) is NOT valid", result_id[i]);
347 ret = vcd_client_get_cmd_from_result_id(result_id[i], &temp_cmd);
348 if (0 == ret && NULL != temp_cmd) {
349 switch (temp_cmd->format) {
350 case VC_CMD_FORMAT_FIXED:
351 /* Nonfixed result is NOT valid */
353 case VC_CMD_FORMAT_FIXED_AND_EXTRA:
354 if (NULL == temp_cmd->parameter) {
355 if (NULL != non_fixed_result) {
356 temp_cmd->parameter = strdup(non_fixed_result);
359 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Parameter (%s)", temp_cmd->parameter);
362 case VC_CMD_FORMAT_EXTRA_AND_FIXED:
363 if (NULL == temp_cmd->command) {
364 if (NULL != non_fixed_result) {
365 temp_cmd->command = strdup(non_fixed_result);
368 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Command type is NOT vaild. Command (%s)", temp_cmd->command);
373 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Unknown command type : %d", temp_cmd->type);
377 if (0 != vc_cmd_list_add(vc_cmd_list, (vc_cmd_h)temp_cmd)) {
378 SLOG(LOG_DEBUG, TAG_VCD, "Fail to add command to list");
379 vc_cmd_destroy((vc_cmd_h)temp_cmd);
382 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] NOT found matached result(%d)", result_id[i]);
386 vc_cmd_print_list(vc_cmd_list);
388 SLOG(LOG_DEBUG, TAG_VCD, "[Server] =========================");
390 int result_count = 0;
391 vc_cmd_list_get_count(vc_cmd_list, &result_count);
393 if (false == vcd_client_manager_get_exclusive()) {
394 int pid = vcd_client_widget_get_foreground_pid();
396 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
397 vcdc_send_show_tooltip(pid, false);
400 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, false);
402 if (-1 != vcd_client_manager_get_pid()) {
403 /* Manager client is available */
404 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
405 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
408 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Manager is NOT available. Send result to client directly");
409 /* Send result to client */
410 ecore_timer_add(0, __vcd_send_selected_result, NULL);
413 /* exclusive command */
414 vc_info_parser_set_result(all_result, event, msg, vc_cmd_list, true);
416 if (-1 != vcd_client_manager_get_pid()) {
417 /* Manager client is available */
418 if (0 != vcdc_send_result_to_manager(vcd_client_manager_get_pid(), VC_RESULT_TYPE_NORMAL)) {
419 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to send result");
422 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Manager is NOT available");
425 vcd_client_manager_set_exclusive(false);
428 vc_cmd_list_destroy(vc_cmd_list, true);
434 * vcd server Interfaces
436 static void __vcd_file_clean_up()
438 SLOG(LOG_DEBUG, TAG_VCD, "== Old file clean up == ");
443 struct dirent *dirp = NULL;
445 dp = opendir(VC_RUNTIME_INFO_ROOT);
447 SLOG(LOG_ERROR, TAG_VCD, "[File message WARN] Fail to open path : %s", VC_RUNTIME_INFO_ROOT);
451 char remove_path[256] = {0, };
453 ret = readdir_r(dp, &entry, &dirp);
455 SLOG(LOG_ERROR, TAG_VCD, "[File ERROR] Fail to read directory");
460 if (!strncmp("vc_", dirp->d_name, strlen("vc_"))) {
461 memset(remove_path, 0, 256);
462 snprintf(remove_path, 256, "%s/%s", VC_RUNTIME_INFO_ROOT, dirp->d_name);
465 if (0 != remove(remove_path)) {
466 SLOG(LOG_WARN, TAG_VCD, "[File message WARN] Fail to remove file : %s", remove_path);
468 SLOG(LOG_DEBUG, TAG_VCD, "[File message] Remove file : %s", remove_path);
472 } while (NULL != dirp);
483 /* Remove old file */
484 __vcd_file_clean_up();
486 /* initialize modules */
487 ret = vcd_config_initialize(__config_lang_changed_cb, __config_foreground_changed_cb, NULL);
489 SLOG(LOG_ERROR, TAG_VCD, "[Server WARNING] Fail to initialize config.");
492 vcd_config_set_service_state(VCD_STATE_NONE);
494 ret = vcd_engine_agent_init(__vcd_server_result_cb);
496 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to engine agent initialize : result(%d)", ret);
500 if (0 != vcd_recorder_create(__server_recorder_callback, __server_recorder_interrupt_callback)) {
501 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to create recorder");
502 return VCD_ERROR_OPERATION_FAILED;
506 ret = vcd_engine_agent_initialize_current_engine();
508 if (VCD_ERROR_ENGINE_NOT_FOUND == ret)
509 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] There is No Voice control engine");
511 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to init engine");
519 if (0 != vcd_engine_agent_load_current_engine()) {
520 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to load current engine");
521 return VCD_ERROR_OPERATION_FAILED;
524 /* Initialize manager info */
525 vcd_client_manager_unset();
527 vcd_config_set_service_state(VCD_STATE_READY);
528 vcdc_send_service_state(VCD_STATE_READY);
530 SLOG(LOG_DEBUG, TAG_VCD, "[Server SUCCESS] initialize");
537 vcd_state_e state = vcd_config_get_service_state();
538 if (VCD_STATE_READY != state) {
539 if (VCD_STATE_RECORDING == state) {
542 vcd_engine_recognize_cancel();
544 if (0 != vcd_recorder_destroy()) {
545 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to destroy recorder");
547 SLOG(LOG_DEBUG, TAG_VCD, "[Server] destroy recorder");
550 if (0 != vcd_engine_agent_release()) {
551 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to release engine");
553 SLOG(LOG_DEBUG, TAG_VCD, "[Server] release engine");
556 vcd_config_set_service_state(VCD_STATE_NONE);
557 vcdc_send_service_state(VCD_STATE_NONE);
559 SLOG(LOG_DEBUG, TAG_VCD, "[Server] mode finalize");
564 static Eina_Bool __finalize_quit_ecore_loop(void *data)
566 SLOG(LOG_DEBUG, TAG_VCD, "[Server] quit ecore main loop");
567 ecore_main_loop_quit();
571 Eina_Bool vcd_cleanup_client(void *data)
573 int* client_list = NULL;
574 int client_count = 0;
578 if (0 == vcd_client_get_list(&client_list, &client_count)) {
579 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up client ");
580 if (NULL != client_list && client_count > 0) {
581 for (i = 0; i < client_count; i++) {
582 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_NORMAL);
585 SLOG(LOG_DEBUG, TAG_VCD, "[Server] pid(%d) should be removed.", client_list[i]);
586 vcd_server_finalize(client_list[i]);
587 } else if (-1 == result) {
588 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
592 SLOG(LOG_DEBUG, TAG_VCD, "=====");
593 SLOG(LOG_DEBUG, TAG_VCD, " ");
595 if (NULL != client_list) {
601 /* If app is in background state, app cannot response message. */
602 if (0 == vcd_client_widget_get_list(&client_list, &client_count)) {
603 SLOG(LOG_DEBUG, TAG_VCD, "===== Clean up widget");
604 if (NULL != client_list && client_count > 0) {
605 for (i = 0; i < client_count; i++) {
606 result = vcdc_send_hello(client_list[i], VCD_CLIENT_TYPE_WIDGET);
609 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget pid(%d) should be removed.", client_list[i]);
610 vcd_server_widget_finalize(client_list[i]);
611 } else if (-1 == result) {
612 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Hello result has error");
616 SLOG(LOG_DEBUG, TAG_VCD, "=====");
617 SLOG(LOG_DEBUG, TAG_VCD, " ");
620 if (NULL != client_list) {
631 int vcd_server_get_service_state()
633 return vcd_config_get_service_state();
639 int vcd_server_mgr_initialize(int pid)
641 if (false == g_is_engine) {
642 if (0 != vcd_engine_agent_initialize_current_engine()) {
643 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
645 return VCD_ERROR_ENGINE_NOT_FOUND;
651 /* check if pid is valid */
652 if (false == vcd_client_manager_is_valid(pid)) {
653 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid(%d) is already exist", pid);
654 return VCD_ERROR_INVALID_PARAMETER;
657 /* Add client information to client manager */
658 if (0 != vcd_client_manager_set(pid)) {
659 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add manager");
660 return VCD_ERROR_OPERATION_FAILED;
663 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager initialize : pid(%d)", pid);
665 return VCD_ERROR_NONE;
668 int vcd_server_mgr_finalize(int pid)
670 /* check if pid is valid */
671 if (false == vcd_client_manager_is_valid(pid)) {
672 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
673 return VCD_ERROR_INVALID_PARAMETER;
676 /* Remove manager information */
677 if (0 != vcd_client_manager_unset()) {
678 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
681 if (0 == vcd_client_get_ref_count()) {
682 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
683 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
686 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Manager Finalize : pid(%d)", pid);
688 return VCD_ERROR_NONE;
691 int vcd_server_mgr_set_command(int pid)
693 if (0 != vcd_client_manager_set_command(pid)) {
694 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
695 return VCD_ERROR_INVALID_PARAMETER;
697 return VCD_ERROR_NONE;
700 int vcd_server_mgr_unset_command(int pid)
702 if (0 != vcd_client_manager_unset_command(pid)) {
703 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
704 return VCD_ERROR_INVALID_PARAMETER;
706 return VCD_ERROR_NONE;
709 int vcd_server_mgr_set_demandable_client(int pid)
711 /* check if pid is valid */
712 if (false == vcd_client_manager_is_valid(pid)) {
713 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
714 return VCD_ERROR_INVALID_PARAMETER;
717 GSList* client_list = NULL;
718 if (0 != vc_info_parser_get_demandable_clients(&client_list)) {
719 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get demandable client");
720 return VCD_ERROR_OPERATION_FAILED;
723 /* Save client list */
724 if (0 != vcd_client_manager_set_demandable_client(pid, client_list)) {
725 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set demandable client list");
726 return VCD_ERROR_OPERATION_FAILED;
729 return VCD_ERROR_NONE;
732 int vcd_server_mgr_set_audio_type(int pid, const char* audio_type)
734 /* check if pid is valid */
735 if (false == vcd_client_manager_is_valid(pid)) {
736 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
737 return VCD_ERROR_INVALID_PARAMETER;
741 vcp_audio_type_e type = VCP_AUDIO_TYPE_PCM_S16_LE;
745 ret = vcd_engine_get_audio_format(audio_type, &type, &rate, &channel);
747 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio format : %d", ret);
748 return VCD_ERROR_OPERATION_FAILED;
751 ret = vcd_recorder_set(audio_type, type, rate, channel);
753 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set audio in type : %d", ret);
754 return VCD_ERROR_OPERATION_FAILED;
757 return VCD_ERROR_NONE;
760 int vcd_server_mgr_get_audio_type(int pid, char** audio_type)
762 /* check if pid is valid */
763 if (false == vcd_client_manager_is_valid(pid)) {
764 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
765 return VCD_ERROR_INVALID_PARAMETER;
768 int ret = vcd_recorder_get(audio_type);
770 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio in type : %d", ret);
771 return VCD_ERROR_OPERATION_FAILED;
774 return VCD_ERROR_NONE;
777 int vcd_server_mgr_set_client_info(int pid)
779 /* check if pid is valid */
780 if (false == vcd_client_manager_is_valid(pid)) {
781 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The manager pid(%d) is NOT valid", pid);
782 return VCD_ERROR_INVALID_PARAMETER;
785 int ret = vcd_client_save_client_info();
787 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to save client info : %d", ret);
788 return VCD_ERROR_OPERATION_FAILED;
791 return VCD_ERROR_NONE;
794 static int __start_internal_recognition()
798 /* 2. Get commands */
799 ret = vcd_client_command_collect_command();
801 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to collect command : %d", ret);
802 return VCD_ERROR_OPERATION_FAILED;
805 ret = vcd_client_get_length();
807 SLOG(LOG_WARN, TAG_VCD, "[Server WARNIING] No current command : %d", ret);
808 return VCD_ERROR_OPERATION_FAILED;
811 /* 3. Set command to engine */
812 ret = vcd_engine_set_commands();
814 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to collect command : %d", ret);
815 return VCD_ERROR_OPERATION_FAILED;
818 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Set command");
820 /* 4. start recognition */
821 ret = vcd_engine_recognize_start(true);
823 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recognition : result(%d)", ret);
824 return VCD_ERROR_OPERATION_FAILED;
827 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start engine");
829 /* 5. recorder start */
830 ret = vcd_recorder_start();
832 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recorder : result(%d)", ret);
833 vcd_engine_recognize_cancel();
837 vcd_config_set_service_state(VCD_STATE_RECORDING);
838 vcdc_send_service_state(VCD_STATE_RECORDING);
840 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Start recognition");
845 static Eina_Bool __vcd_request_show_tooltip(void *data)
847 int pid = vcd_client_widget_get_foreground_pid();
849 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
850 vcdc_send_show_tooltip(pid, (bool)data);
856 int vcd_server_mgr_start(vcd_recognition_mode_e recognition_mode, bool exclusive_cmd, bool start_by_client)
858 /* 1. check current state */
859 vcd_state_e state = vcd_config_get_service_state();
861 if (VCD_STATE_READY != state) {
862 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not ready");
863 return VCD_ERROR_INVALID_STATE;
866 SLOG(LOG_DEBUG, TAG_VCD, "[Server] set recognition mode = %d", recognition_mode);
867 vcd_client_set_recognition_mode(recognition_mode);
869 if (false == exclusive_cmd) {
870 /* Notify show tooltip */
871 int pid = vcd_client_widget_get_foreground_pid();
873 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip show and widget command");
874 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
878 vcd_client_manager_set_exclusive(exclusive_cmd);
882 if (true == start_by_client) {
883 /* Get foreground pid */
884 if (0 != vcd_config_get_foreground(&fg_pid)) {
885 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get foreground");
888 /* Set client exclusive option */
889 if (0 != vcd_client_set_exclusive_command(fg_pid)) {
890 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command");
894 int ret = __start_internal_recognition();
896 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
900 if (true == start_by_client) {
901 vcd_client_unset_exclusive_command(fg_pid);
904 return VCD_ERROR_NONE;
907 int vcd_server_mgr_stop()
909 /* 1. Check current state is recording */
910 if (VCD_STATE_RECORDING != vcd_config_get_service_state()) {
911 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Current state is not recording");
912 return VCD_ERROR_INVALID_STATE;
915 /* 2. Stop recorder */
918 /* 3. Stop engine recognition */
919 int ret = vcd_engine_recognize_stop();
921 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to stop recognition : %d", ret);
924 /* 4. Set original mode */
925 vcd_config_set_service_state(VCD_STATE_PROCESSING);
926 vcdc_send_service_state(VCD_STATE_PROCESSING);
928 return VCD_ERROR_NONE;
931 int vcd_server_mgr_cancel()
933 /* 1. Check current state */
934 vcd_state_e state = vcd_config_get_service_state();
935 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
936 SLOG(LOG_WARN, TAG_VCD, "[Server ERROR] Current state is not recording or processing");
937 return VCD_ERROR_INVALID_STATE;
940 /* 2. Stop recorder */
942 /* 3. Cancel engine */
943 int ret = vcd_engine_recognize_cancel();
945 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to cancel : %d", ret);
948 if (false == vcd_client_manager_get_exclusive()) {
949 int pid = vcd_client_widget_get_foreground_pid();
951 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Request tooltip hide");
952 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
955 vcd_client_manager_set_exclusive(false);
959 vcd_config_set_service_state(VCD_STATE_READY);
960 vcdc_send_service_state(VCD_STATE_READY);
962 return VCD_ERROR_NONE;
966 int vcd_server_mgr_result_select()
968 __vcd_send_selected_result(NULL);
970 return VCD_ERROR_NONE;
974 * VC Server Functions for Client
976 int vcd_server_initialize(int pid)
978 if (false == g_is_engine) {
979 if (0 != vcd_engine_agent_initialize_current_engine()) {
980 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
982 return VCD_ERROR_ENGINE_NOT_FOUND;
988 if (false == vcd_engine_is_available_engine()) {
989 if (0 != vcd_engine_agent_initialize_current_engine()) {
990 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
991 return VCD_ERROR_ENGINE_NOT_FOUND;
995 /* check if pid is valid */
996 if (true == vcd_client_is_available(pid)) {
997 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
998 return VCD_ERROR_INVALID_PARAMETER;
1001 /* Add client information to client manager */
1002 if (0 != vcd_client_add(pid)) {
1003 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1004 return VCD_ERROR_OPERATION_FAILED;
1007 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Initialize : pid(%d)", pid);
1009 return VCD_ERROR_NONE;
1012 int vcd_server_finalize(int pid)
1014 /* check if pid is valid */
1015 if (false == vcd_client_is_available(pid)) {
1016 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1017 return VCD_ERROR_INVALID_PARAMETER;
1020 /* Remove client information */
1021 if (0 != vcd_client_delete(pid)) {
1022 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1025 if (0 == vcd_client_get_ref_count()) {
1026 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Connected client list is empty");
1027 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1030 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Client Finalize : pid(%d)", pid);
1032 return VCD_ERROR_NONE;
1035 int vcd_server_set_command(int pid, vc_cmd_type_e cmd_type)
1037 /* check if pid is valid */
1038 if (false == vcd_client_is_available(pid)) {
1039 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1040 return VCD_ERROR_INVALID_PARAMETER;
1043 if (0 != vcd_client_set_command_type(pid, cmd_type)) {
1044 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1045 return VCD_ERROR_OPERATION_FAILED;
1051 int vcd_server_unset_command(int pid, vc_cmd_type_e cmd_type)
1053 /* check if pid is valid */
1054 if (false == vcd_client_is_available(pid)) {
1055 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1056 return VCD_ERROR_INVALID_PARAMETER;
1059 if (0 != vcd_client_unset_command_type(pid, cmd_type)) {
1060 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset command type : pid(%d), cmd_type(%d)", pid, cmd_type);
1061 return VCD_ERROR_OPERATION_FAILED;
1068 int vcd_server_set_exclusive_command(int pid, bool value)
1070 /* check if pid is valid */
1071 if (false == vcd_client_is_available(pid)) {
1072 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1073 return VCD_ERROR_INVALID_PARAMETER;
1076 if (true == value) {
1077 if (0 != vcd_client_set_exclusive_command(pid)) {
1078 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to set exclusive command : pid(%d)", pid);
1079 return VCD_ERROR_OPERATION_FAILED;
1082 if (0 != vcd_client_unset_exclusive_command(pid)) {
1083 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to unset exclusive command : pid(%d)", pid);
1084 return VCD_ERROR_OPERATION_FAILED;
1091 int vcd_server_request_start(int pid, bool stop_by_silence)
1093 /* check if pid is valid */
1094 if (false == vcd_client_is_available(pid)) {
1095 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid(%d) is NOT forground client", pid);
1096 return VCD_ERROR_INVALID_PARAMETER;
1100 /* Check current state */
1101 vcd_state_e state = vcd_config_get_service_state();
1103 /* Service state should be ready */
1104 if (VCD_STATE_READY != state) {
1105 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
1106 return VCD_ERROR_INVALID_STATE;
1109 if (-1 != vcd_client_manager_get_pid()) {
1110 /* Check current pid is valid */
1111 if (false == vcd_client_manager_check_demandable_client(pid)) {
1112 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
1113 return VCD_ERROR_INVALID_PARAMETER;
1117 ret = vcd_server_mgr_start(stop_by_silence, false);
1119 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1120 return VCD_ERROR_INVALID_PARAMETER;
1126 int vcd_server_request_stop(int pid)
1129 /* Check current state */
1130 vcd_state_e state = vcd_config_get_service_state();
1132 /* Service state should be ready */
1133 if (VCD_STATE_RECORDING != state) {
1134 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording : pid(%d)", pid);
1135 return VCD_ERROR_INVALID_STATE;
1138 if (-1 != vcd_client_manager_get_pid()) {
1139 /* Check current pid is valid */
1140 if (false == vcd_client_manager_check_demandable_client(pid)) {
1141 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
1142 return VCD_ERROR_INVALID_PARAMETER;
1146 ret = vcd_server_mgr_stop();
1148 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1149 return VCD_ERROR_OPERATION_FAILED;
1152 return VCD_ERROR_NONE;
1155 int vcd_server_request_cancel(int pid)
1158 /* Check current state */
1159 vcd_state_e state = vcd_config_get_service_state();
1161 /* Service state should be recording or processing */
1162 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1163 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
1164 return VCD_ERROR_INVALID_STATE;
1167 if (-1 != vcd_client_manager_get_pid()) {
1168 /* Check current pid is valid */
1169 if (false == vcd_client_manager_check_demandable_client(pid)) {
1170 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current client is NOT available : pid(%d)", pid);
1171 return VCD_ERROR_INVALID_PARAMETER;
1175 ret = vcd_server_mgr_cancel();
1177 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1178 return VCD_ERROR_OPERATION_FAILED;
1181 return VCD_ERROR_NONE;
1186 * VC Server Functions for Widget lib
1188 int vcd_server_widget_initialize(int pid)
1190 if (false == g_is_engine) {
1191 if (0 != vcd_engine_agent_initialize_current_engine()) {
1192 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1193 g_is_engine = false;
1194 return VCD_ERROR_ENGINE_NOT_FOUND;
1200 if (false == vcd_engine_is_available_engine()) {
1201 if (0 != vcd_engine_agent_initialize_current_engine()) {
1202 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] No Engine");
1203 return VCD_ERROR_ENGINE_NOT_FOUND;
1207 /* check if pid is valid */
1208 if (true == vcd_client_widget_is_available(pid)) {
1209 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] The pid is already exist");
1210 return VCD_ERROR_INVALID_PARAMETER;
1213 /* Add client information to client manager */
1214 if (0 != vcd_client_widget_add(pid)) {
1215 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to add client info");
1216 return VCD_ERROR_OPERATION_FAILED;
1219 SLOG(LOG_DEBUG, TAG_VCD, "[Server Success] Initialize widget : pid(%d)", pid);
1221 return VCD_ERROR_NONE;
1224 int vcd_server_widget_finalize(int pid)
1226 /* check if pid is valid */
1227 if (false == vcd_client_widget_is_available(pid)) {
1228 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1229 return VCD_ERROR_INVALID_PARAMETER;
1232 /* Remove client information */
1233 if (0 != vcd_client_widget_delete(pid)) {
1234 SLOG(LOG_WARN, TAG_VCD, "[Server WARNING] Fail to delete client");
1237 if (0 == vcd_client_get_ref_count()) {
1238 SLOG(LOG_DEBUG, TAG_VCD, "[Server] connected client list is empty");
1239 ecore_timer_add(0, __finalize_quit_ecore_loop, NULL);
1242 return VCD_ERROR_NONE;
1245 int vcd_server_widget_start_recording(int pid, bool widget_command)
1247 /* check if pid is valid */
1248 if (false == vcd_client_widget_is_available(pid)) {
1249 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] pid is NOT valid ");
1250 return VCD_ERROR_INVALID_PARAMETER;
1253 if (true == widget_command) {
1254 vcd_client_widget_set_command(pid);
1255 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is available");
1257 vcd_client_widget_unset_command(pid);
1258 SLOG(LOG_DEBUG, TAG_VCD, "[Server] widget command is NOT available");
1261 int ret = __start_internal_recognition();
1263 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to start recongition : %d", ret);
1264 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)false);
1270 int vcd_server_widget_start(int pid, bool stop_by_silence)
1272 /* check if pid is valid */
1273 int fore_pid = vcd_client_widget_get_foreground_pid();
1274 if (pid != fore_pid) {
1275 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
1276 return VCD_ERROR_INVALID_PARAMETER;
1279 /* Check current state */
1280 vcd_state_e state = vcd_config_get_service_state();
1282 /* Service state should be ready */
1283 if (VCD_STATE_READY != state) {
1284 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Ready : pid(%d)", pid);
1285 return VCD_ERROR_INVALID_STATE;
1288 vcd_client_set_slience_detection(stop_by_silence);
1290 /* Notify show tooltip */
1291 ecore_timer_add(0, __vcd_request_show_tooltip, (void*)true);
1296 int vcd_server_widget_stop(int pid)
1298 /* check if pid is valid */
1299 int fore_pid = vcd_client_widget_get_foreground_pid();
1300 if (pid != fore_pid) {
1301 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
1302 return VCD_ERROR_INVALID_PARAMETER;
1306 /* Check current state */
1307 vcd_state_e state = vcd_config_get_service_state();
1309 /* Service state should be recording */
1310 if (VCD_STATE_RECORDING != state) {
1311 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current service state is not Recording : pid(%d)", pid);
1312 return VCD_ERROR_INVALID_STATE;
1315 ret = vcd_server_mgr_stop();
1317 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to start recognition");
1318 return VCD_ERROR_OPERATION_FAILED;
1321 return VCD_ERROR_NONE;
1324 int vcd_server_widget_cancel(int pid)
1326 /* check if pid is valid */
1327 int fore_pid = vcd_client_widget_get_foreground_pid();
1328 if (pid != fore_pid) {
1329 SLOG(LOG_ERROR, TAG_VCD, "[Server] pid is NOT foreground");
1330 return VCD_ERROR_INVALID_PARAMETER;
1334 /* Check current state */
1335 vcd_state_e state = vcd_config_get_service_state();
1337 /* Service state should be recording or processing */
1338 if (VCD_STATE_RECORDING != state && VCD_STATE_PROCESSING != state) {
1339 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Current state is not Recording or Processing : pid(%d)", pid);
1340 return VCD_ERROR_INVALID_STATE;
1343 ret = vcd_server_mgr_cancel();
1345 SLOG(LOG_DEBUG, TAG_VCD, "[Server] Fail to cancel recognition : %d", ret);
1349 return VCD_ERROR_NONE;